SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 49
  1. #1
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    combining 2 scripts

    hi all
    i have 2 javascript one is simple login form and other is username function.
    i want to fit username function into login form because username function will allow special characters.
    below is my login form ..
    <html>
    <head>
    <meta charset="utf-8">
    <title>Simple Registration Form</title>
    <script type="text/javascript">
    function checkName(form) /* for real name verification */
    {
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
    var isCorrectFormat = oRE.test(text);
    if (!isCorrectFormat)
    {
    alert("Invalid characters in username. It can only contain...");
    return false;
    }


    if (form.realname.value == '')
    {
    alert('Error: Username cannot be blank!');
    form.realname.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, 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('Successfull.');
    return true;
    }
    }
    }

    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()">
    Name: <input type="text" name="realname" size="25">
    <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>

    i want to include the below one in the above function so that it works perfectly
    <html>
    <head>
    <title> Validate Username</title>
    <script type="text/javascript">
    function validateUsername(form)
    {
    var sUsername = form.value;
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
    var isCorrectFormat = oRE.test(sUsername);
    if (!isCorrectFormat)
    {
    alert("Incorrect format.");
    textbox.select();
    textbox.focus();
    return false;
    }
    alert("Correct format");
    return true;
    }
    </script>
    </head>
    <body>
    <form>
    <input type="text" id="txtUsername" size="30" maxlength="20" />&nbsp;<input type="button"
    value="Validate" onclick="validateUsername(document.getElementById('txtUsername'));" />
    </form>
    </body>
    </html>
    please can you make for that me..

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Please put code tags around the code you post, so it's easier to read and understand.

    And it looks to me like you already have the exact same check on name in the first script. What is it that the second script does, and the first doesn't?

  3. #3
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first script is a complete registration form with username not accepting special characters.then i have written the second script exclusively for username accepting atleast one special characters just to check working or not.
    it is working perfectly so i need to replace the checkname function in first form with second script i have written.
    so that only one script will be there to accept username with at least one special characters..

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i; <-- first script
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i; <-- second script

    I see no difference between the two

  5. #5
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no replace whole function in second script(which has username)
    in the first function(only checkname(form) function) so that i need only one
    code for simple login form

  6. #6
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can u tell me why the following script it is not executing..
    where i have gone wrong...
    <html>
    <head>
    <meta charset="utf-8">
    <title>Validation using JavaScript</title>
    <script type="text/javascript">
    function checkname(form)
    {
    {
    var sRealname = form.value;
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
    var isCorrectFormat = oRE.test(sRealname);
    if (!isCorrectFormat)
    {
    alert("Incorrect format.");
    textbox.select();
    textbox.focus();
    return false;
    }
    alert("Correct format");
    return true;
    }
    else 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;
    }

    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>

  7. #7
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your checkname is the reason your script isn't working, you have a random opening curly brace and else if statement. See the below code for the function snippet of how the code should look.

    Code JavaScript:
    function checkname(form)
    {
        var sRealname = form.value;
        var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
        var isCorrectFormat = oRE.test(sRealname);
     
        if (!isCorrectFormat)
        {
            alert("Incorrect format.");
            textbox.select();
            textbox.focus();
            return false;
        }
        else
        {
            alert("Correct format");
            return true;
        }
     
        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;
        }
     
        return true;
    }
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Please put code tags around the code you post, so it's easier to read and understand.
    I repeat myself...

  9. #9
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <the function given by you is not working >
    <below is the overall javascript function for simple form.it has some errors tell me what should i do to execute the below script>
    <simple login form>

    <html>
    <head>
    <meta charset="utf-8">
    <title>Validation using JavaScript</title>
    <script type="text/javascript">
    function checkname(form)
    {
    var sRealname = form.value;
    var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
    var isCorrectFormat = oRE.test(sRealname);

    if (!isCorrectFormat)
    {
    alert("Incorrect format.");
    textbox.select();
    textbox.focus();
    return false;
    }
    else
    {
    alert("Correct format");
    return true;
    }

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

    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>

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Code:
    var sRealname = form.realname.value;

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    the function given by you is not working
    below is the overall javascript function for simple form.it has some errors tell me what should i do to execute the below script
    "is not working"
    "has some errors"

    are not very helpful descriptions of your problem.

    When you get errors, post the error messages.
    When it's "not working", explain what that means.

    And put code tags around the code you post here!

  12. #12
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <no it is not accepting.i have given var sRealname = form.realname.value;>
    is it right

  13. #13
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <even if we give wrong output to username as ravi!951 is is not displaying error message>

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    no it is not accepting.
    Accepting what?

    By the way, if you go to the advanced editor when writing your posts, there is a 'select syntax' drop down in the editor's menu bar, where you can choose the coding language, and the appropriate code tags will be inserted.

  15. #15
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <even if we give wrong output to username as ravi!951 is is not displaying error message as incorrect format>

  16. #16
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    There is no need to put < and > around everything you write. I only told you to put code tags around the code you post. The general code tags are [ code ] and [ /code ] (without the spaces). If you go to the advanced reply editor, you can choose more language specific tags in the 'select syntax' drop down box in the editor's menu.

    Please post your corrected code once again (with the code tags around it )

  17. #17
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    below is my corrected code
    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 (!isCorrectFormat)
        {
            alert("Incorrect format.");
            textbox.select();
            textbox.focus();
            return false;
        }
        else
        {
            alert("Correct format");
            return true;
        }
        
        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;
        }
        
        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>
    Last edited by guido2004; Jul 21, 2011 at 02:39. Reason: put the correct [code] tags around the code

  18. #18
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    function checkname(form)
    if (!checkName(form)

    Do you see the difference?

  19. #19
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya got it.but when i enter username like ravi951(valid) it is displaying correct format and another alert message "invalid email address,please re-enter".
    i dont want to display both.

  20. #20
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Get rid of the 'correct format' message. Did you notice that right now, your code doesn't check anymore if the username is empty or less than 4 characters? Try it.

  21. #21
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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..

  22. #22
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    You could change the order of the checks:
    Code 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;
     }

  23. #23
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have changed the order and executed the script.but now it is displaying nothing...
    what's problem.

  24. #24
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Post the modified script.

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

  25. #25
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have given username as ravi!kumar but it is not displaying any thing
    below is my modified code.....

    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>
    Last edited by guido2004; Jul 21, 2011 at 03:59. Reason: code tags


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •