Check for Space character in input with Jquery

AndrewLeonardi

I have a form where a user will set up a new Username. The issue is user's have been creating username's with a space and I want to avoid that.

I have been able to detect when there is a space using the following:

var hasSpace = $('#usernameValue').val().indexOf(' ')>=0;

However I cannot figure out to have the form check for that space when the user is typing in that input section.

How would I change my code to do this?

Thank you!

Bergur

As suggested you can use the keyup function. This example checks if you entered space and alerts the user

$('#usernameValue').keyup(function(e) {   
   if (e.which === 32)  {
     alert('you entered space');
     // Do whatever logic is needed here
   }
});

Working JsFiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JQuery Input Validation: How To Check Each Input For Empty or Character or Number Without form submit

Check if input contains an invalid character

Julia - Check if every character in string is lowercase or space

Check if character in string has space behind

How can I check if the character equal space?

Avoid backslashing of space character in GNU parallel input

Check if input is empty with jquery

Check If Input is empty JQuery

Jquery check input trim

How to check if input is white space and null or not?

Loop to check user's input for empty space

Check character from input count occurance in String

GNU grep check if input contains a LF character

Regular expression check allowing character input

C++, check if input value is a specific character

how to check if user input is a certain character

Check if String contains tab plus any non white space character

How to check if value contains at least one character, expect for space

jQuery check if an input is type checkbox?

Regexp for finding out space and '/' character in user input using actionscript 3

Why I can't use space character in input?

Space above input, jQuery + Css3

JQuery Validation - Check if the textbox has special character

Select first character of input value with jQuery

jQuery .val() ignoring first character of input

jQuery if input value matches character in div

Replace a # character with a space and a # character

Not allow space as a first character and allow only letters using jquery

How can I check if the user input is a word and not a special character like (*!?)?