Username verification

Well with what you have there, it could be:


/^[a-z]{4}[._-]\\d{3}/i

so numbers also
/[1][0-9]{4}[._-]\d{3}/i
right


  1. a-z ↩︎

Right.


  1. a-z ↩︎

now for every character it is alerting
Invalid characters in username. It can only contain…
if we give ravi also it is not accepting.
check it once please…

function checkName(form) /* for real name verification */
{
var re =/[1][0-9]{4}[._-]\d{3}/i;
var isCorrectFormat = re.test(form);
if (!isCorrectFormat)
{
alert(“Invalid characters in username. It can only contain…”);
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;
}

  1. a-z ↩︎

The characters and digits need to be within the same grouping class.

where can u modify it please…

You can modify it by placing the digits in to the same class as the characters.

i am not getting you

Here’s some documentation on what a character class is.
Character Class or Character Sets

means i need to give [a-z0-9._-]
so that it should match only these characters.
in username

I have absolutely no idea on regular expressions.
please can u modify that for me …

Before you had [a-z][0-9]

Putting them both in the same character class means to do this:
[a-z0-9]

whether it will work.below is the given code
and my username should also work with .(dot),_(underscore),-(hypen)

function checkName(form) /* for real name verification */
{
var re =[a-z0-9];
var isCorrectFormat = re.test(form);
if (!isCorrectFormat)
{
alert(“Invalid characters in username. It can only contain…”);
return false;
}

Let me try to explain this again, in simpler terms that you might find easier to understand.

Before you had this non-working code:


var re =/^[a-z][0-9]{4}[._-]\\d{3}/i;

Where you have [a-z][a-9] you should instead have [a-z0-9]

Does that make sense to you?

do you think like below written one…
do u think this will work

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

What does testing it reveal to you?

for anything we give username a valid also id is displaying error say
usernae is ravi for this also it is displaying error.

Something must be wrong with my linguistic interpreter, because “username a valid also id” doesn’t seem to parse in to anything vaguely understandable.

Can you please repeat in a way that can be understood, and perhaps let us know what you are actually testing?

actually i am working on a simple registration form that has 5 fields as username,email id,password,retype password and phone number.
where username should be atleast 4 characters long including special characters like .(dot),_(underscore),-(hypen).
email id should be of valid one
and password and retype password should match
phone no should be of 10 digits …

here is my complete code
i find a problem in testing for username and password field.if i enter password field of length less than 6 characters it should display alert message i have written as the length should be atleast 6 characters but it is asking to enter for retype password field and then displaying the message “it should be of length 6 characters”(given password is less than 6 characters)
can u check it once where i have gone wrong.

<html>
<head>
<meta charset=“utf-8”>
<title>Validation using JavaScript</title>
<script type=“text/javascript”>
function checkName(form) /* for real name verification */
{
var re =/[1]{4}[.-]\d{3}/i;
var isCorrectFormat = re.test(form);
if (!isCorrectFormat)
{
alert("Invalid characters in username. It can only contain.
and-");
return false;
}

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;
}

function checkEmail(form)   /* for email validation */
{
    if (/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/.test(form.email.value))
    {
        return true;
    }
    
     alert('Invalid E-mail Address! Please re-enter.'); 
     return false;
}

function validatePwd(form)       /* password & retype-password verification */
{
    var invalid = ' '; 
    minLength = 6;
    var pw1 = form.password.value; 
    var pw2 = form.password2.value;
    
     
    if (pw1 == '' || pw2 == '')
    {
        alert('Please enter your password twice.');
        return false;
    }                                              
    
   
    if (form.password.value.length &lt; minLength)
    {
        alert('Your password must be at least ' + minLength + ' characters long. Try again.');
        return false;
    }                                                                              
    
    if (document.form.password.value.indexOf(invalid) &gt; -1)
    {
        alert('Sorry, spaces are not allowed.');
        return false;
    }
    else
    {
        if (pw1 != pw2)
        {
            alert('You did not enter the same new password twice. Please re-enter your password.');
            return false;
           }
        else
         {
            alert('Passwords Match.');
               return false;
         } 
                                                                                                   
        return false;                                                                    
    }
}  

function validPhone(form)          /* phone no validation */
{
    var valid = '0123456789'; 
    phone = form.phoneno.value;
    
    if (phone == '')
    {
        alert('This field is required. Please enter phone number');
        return false;
    }
    
    if (!phone.length &gt; 1 || phone.length &lt; 10)
    {
        alert('Invalid phone number length! Please try again.');
        return false;
    }
    
    for (var i = 0; i &lt; phone.length; i++)
    {
        temp = '' + phone.substring(i, i + 1);
        
        if (valid.indexOf(temp) == -1)
        {
            alert('Invalid characters in your phone. Please try again.');
            return false;
        }
    }
    
    return true;
}

function validate()
{
    var form = document.forms['form'];
    
    if (!checkName(form) || !checkEmail(form) || !validatePwd(form) || !validPhone(form))
    {
        return false;
    }
    
    return true;
}

</script>
</head>
<body>

<form action=“” method=“post” name=“form” onsubmit=“return validate()”>
User Name : <input type=“text” name=“realname” size=“19”>
<br>
E-Mail : <input type=“text” name=“email” size=“25”>
<br>
Password : <input type=“password” name=“password” maxlength=“12” size=“25”>
<br>
Retype password: <input type=“password” name=“password2” maxlength=“12” size=“25”>
<br>
PhoneNo : <input type=“phoneno” name=“phoneno” maxlength=“10” size=“25”>
<br>
<input type=“submit” value=“Submit”>
</form>

</body>
</html>


  1. a-z0-9 ↩︎

I’m currently suffering from a head-hammering cold, so you should go back to the code that you got from the other guy.