How to remove space and restrict special chars using jquery?

I am creating a signup form in my website. I want to implement some checkup with username, which are

  • username can't have any space,
  • username can't have any special characters other than dot (.) as gmail is doing in thier signup form.

I am using jQUERY in my app, can anyone tell me how to implement above listed two checkups using jquery ?? I remember these two main checkups for username please suggest if you knows any other?




Have a look at the validation plug-in.

Whatever you do, validate the username at the server side too.


You can check that using javascript regex:

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

First see here in another entry in 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 using jQuery.

You can also check out this page for a another jQuery solution.


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

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



When I checked the answers, I saw that space is allowed. So I improve the regex for others that will read this question:

You need to do like this:

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

Description of regex:

^ -- Start of the line

[a-z0-9_-] -- Match characters and symbols in the list, a-z, 0-9, underscore, hyphen

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

$ -- End of the line


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.