Combining 2 scripts

yes i checked.when i give empty value also it is displaying “incorrect format”.but i want it to display as “username cannot be blank”.
what should i do now…

You could change the order of the checks:


function checkname(form)
 {
    var sRealname = form.realname.value;
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
    var isCorrectFormat = oRE.test(sRealname);
    
    if (sRealName == '')
    {
        alert('Error: Username cannot be blank!');
        form.realname.focus();
        return false;
    }
    else if (sRealName.length < 4)
    {
        alert("UserName should be atleast 4 characters long");
        return false;
    }
    else if (!isCorrectFormat)
    {
        alert("Incorrect format.");
        textbox.select();
        textbox.focus();
        return false;
    }   
    
    return true;
 }

i have changed the order and executed the script.but now it is displaying nothing…
what’s problem.

Post the modified script.

And tell me what values you put for username and the result of the tests.

i have given username as ravi!kumar but it is not displaying any thing
below is my modified code…

<html>
<head>
<meta charset="utf-8">
<title>Validation using JavaScript</title>
<script type="text/javascript">
   function checkName(form)
 {
    var sRealname = form.realname.value;
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
    var isCorrectFormat = oRE.test(sRealname);
    
    if (sRealName == '')
    {
        alert('Error: Username cannot be blank!');
        form.realname.focus();
        return false;
    }
    else if (sRealName.length < 4)
    {
        alert("UserName should be atleast 4 characters long");
        return false;
    }
    else if (!isCorrectFormat)
    {
        alert("Incorrect format.");
        textbox.select();
        textbox.focus();
        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 < minLength)
        {
            alert('Your password must be at least ' + minLength + ' characters long. Try again.');
            return false;
        }                                                                              
        
        if (document.form.password.value.indexOf(invalid) > -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 > 1 || phone.length < 10)
        {
            alert('Invalid phone number length! Please try again.');
            return false;
        }
        
        for (var i = 0; i < 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>

var sRealname
oRE.test(sRealname)
if (sRealName == ‘’)
else if (sRealName.length < 4)

You really need to pay more attention to your variable names. Javascript is case sensitive.

i have both password and retype password field right,
when i give password as ravi it should say password should be atleast 6 characters long but instead it is asking enter retype password ,after entering both only it is checking.but i want to check for password field if it is less than 6 characters it should display the message “password should be atleast 6 characters long”…
what to do…

It depends on the order of the checks. If you look at your code, you’ll see that the first password check checks if one or both of the password fields are empty.
If you want another check to be performed first, you’ll have to move it up.

By the way, right now the password check function always returns false…

then i need to use length function to check the password length for password field right…

You already have the correct check for the length, but it’s executed after the empty password field check. Change the order of the checks if you want to have the length check first.

how to change the order of the password function…

:eek:
Just move the check you want to move above or below another check? You know, cutting and pasting the piece of code that performs the check you want to move?

i have changed but also not accepting…

function validatePwd(form) /* password & retype-password verification*/
{
var invalid = ’ ';
minLength = 6;
var pw1 = form.password.value;
var pw2 = form.password2.value;

if (form.password.value.length &lt; minLength)
    {
        alert('Your password must be at least ' + minLength + ' characters long. Try again.');
        return false;
    }                                                                                      

if (pw1 == ‘’ || pw2 == ‘’)
{
alert(‘Please enter your password twice.’);
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;              
	}
}

What do you mean?

Like I told you before, you have to explain what test you are doing. The values, the result, the desired result.

after entering valid username and valid email id fields and clicking the submit button it should ask for enter password field but when i click submit button
the username and email id passwords are not displayed

It works fine for me.
It tells me the password has to be more at least 6 characters long.

no before entering for password field only it is displaying the message “password should be atleast six characters long”

Yes, that’s what you wanted:

when i give password as ravi it should say password should be atleast 6 characters long but instead it is asking enter retype password

ya that is what i asked you whether to include the length field for password.

You already do that. What do you think this does?

if (form.password.value.length < minLength)