How to remove space and limit special characters using jquery?

I am creating a registration form on my website. I want to do some validation with the username that

  • The user cannot have a space,
  • The username cannot contain any special characters other than a period (.), As gmail follows its registration form.

I am using jquery in my application, can anyone tell me how to implement the above two validations using jquery ?? I remember these two basic checks for username, please suggest if you know any other?

thanks

+1


a source to share


5 answers


I tried out the following jquery script by examining the links ( this one ) given by @Dror to check for incorrect characters in the username,

if (!$('#uname').val().match("^[a-z0-9'.\s]{1,50}$")) {
    //not a correct username
}

      



thanks

0


a source


Have a look at the validation plugin .



Whatever you do, confirm the username and server side.

+4


a source


You can check that with javascript regex:

if ($('#username').val().match(/^(\w)+[\w\d\.]*/)) {
  // everyting is ok
}else {
  // something is wrong
}

      

+2


a source


First see here in another post on SO, jquery-validate-how-to-add-a-rule-for-regular-expression-validation .

if you need other ideas, try here for an example using PHP and an AJAX call with jQuery.

You can also check this page for another jQuery solution.

+1


a source


When I checked the answers, I saw that the space was allowed. So I'm improving the regex for others who will be reading this question:

You need to do the following:

if (!$('#uname').val().match(/^[a-z0-9_-]{3,15}$/)) {
    alert("Please, use standard characters.");
}

      

Regular expression description:

^ - beginning of line

[a-z0-9_-] - match characters and characters in the list, az, 0-9, underscore, hyphen

{3.15} - Length of at least 3 characters and maximum length of 15

$ - End of line

0


a source







All Articles