Username verification

Hi all,
I have written a function for accepting name and should be atleast minimum 4 characters.
It is accepting all characters including special characters.
Now i want my function to accept only a-z0-9 and _(underscore),-(hyphen)
and .(dot)
Below is my function

function checkName(form) /* for real name verification */
{
if (form.realname.value == ‘’)
{
alert(‘Error: Username cannot be blank!’);
form.realname.focus();
return false;
}
else if(form.realname.value.length < 4)
{
alert(“UserName should be atleast 4 characters long”);
return false;
}

    return true;
}

Do you think that a regular expression would be useful to achieve that?

first Congratulations for being javascript of the year 2010.

any thing as long as the function is correct to accept the special characters as .(dot),-(hypen),_(underscore)

Here’s something that should help you then. It’s a regular expression that matches anything that is not alphabetic, or a dash or an underscore.


/[^a-z_-]/i

The [url=“https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec” documentation page shows you how to use it.

If it matches anything, those things that it matches are characters that are not allowed.

i need to accept the above three special characters in username for my function
to fit

Then you can add the full stop to the list of characters.


/[^a-z_.-]/i

then numbers also i can add right 0-9 as
/[^a-z0-9_.-]/i

Correct.

whether is this also correct

var oRE = /[1]{4,}$/g;
var isCorrectFormat = oRE.test(text);
if (!isCorrectFormat)
{
alert(“Invalid characters in username. It can only contain…”);
return false;
}

/[^a-z0-9_.-]/i


  1. \w\d-. ↩︎

Yes, that is also another way to do it.

Thank u

hi dude the function is not working
it is accepting other functions such as !,@,# and so on other than .,_,-
below is the code i have written…


function checkName(form)          /* for real name verification */
   {
     var oRE =/[^a-z0-9_.-]/i;
     var isCorrectFormat = oRE.test(form);
      if (!isCorrectFormat)
       {
         alert("Invalid characters in username. It can only contain 0-91-9._-");
         return false;
        }

  else  if (form.realname.value == '')
       {
           alert('Error: Username cannot be blank!');
            form.realname.focus();
           return false;
       }
               else if(form.realname.value.length < 4)
               {
               alert("UserName should be atleast 4 characters long");
               return false;
               }


       return true;
}

The code you got from the other chap checks for valid characters. The regular expression I was helping you with checks for invalid characters.

Do not mix.

means the code you given will work for other than a-z0-9and .,_,-
is that so

pardon?

means the regular expression u given will work other than that 0-9a-z and .(dot), -(hypen),_(underscore) ?
my problem is it should like something like this
username is ravi_951 or ravi.951 or ravi-951
u find in gmail right accepting _ and .

No, that’s not the right. His regular expression checks that all the characters are valid. The regular expression that I was helping you with checks if any of the characters are not valid.

Do you understand the difference?

His was checking that only cats, dogs, and chickens were present.
Mine was checking if any of them were not cats, dogs, or chickens.

Both achieve the same end goal, just from different directions.

then if my username is ravi@951 it should not work right
but the above function is working for that.

If one is checking for a positive match, and another is checking for a negative match, you cannot just swap them around like you did. There is more to crafting it together than you currently understand, or sadly, that I at nearly midnight have the time to convey.

k then give the regular expression to check that username should only contain
only 3 special characters as .(dot),_(underscore) and-(hypen).
as such ravi.951 and ravi_951 and ravi-951 should work.