1

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?

Thanks

djmzfKnm
  • 26,679
  • 70
  • 166
  • 227

5 Answers5

4

Have a look at the validation plug-in.

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

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • Very good point - you shouldn't rely solely on client side validation as it can easily be by-passed +1. – Ian Oxley May 12 '09 at 12:21
3

You can check that using javascript regex:

if ($('#username').val().match(/^(\w)+[\w\d\.]*/)) {
  // everyting is ok
}else {
  // something is wrong
}
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
1

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.

Community
  • 1
  • 1
Dror
  • 7,255
  • 3
  • 38
  • 44
0

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

Erman Belegu
  • 4,074
  • 25
  • 39
0

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
}

Thanks

Community
  • 1
  • 1
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227