Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Where(Why) Its going wrong

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Registration Form</title>
    <script>
    function register(){
    var name=document.testform.name.value;
    var email=document.testform.email.value;
    var phone=document.testform.phone.value;
    var password=document.testform.password.value;
    if(name==null ||name=="")
    {
    alert('Please enter UserName.');
    document.testform.name.focus();
    return false;
    }
    if(email==null || email=="")
    {
    alert('Please enter email.');
    document.testform.email.focus();
    return false;
    }
    if(phone==null || phone=="")
    {
    alert('Please enter phone');
    document.testform.phone.focus();
    return false;
    }
    if(password==null || password=="")
    {
    alert('No password?');
    document.testform.password.focus();
    return false;
    }
    return true;
    }
    </script>
    </head>

    <body>
    <form action="" method="post" name="testform" target="_blank" onsubmit="return register();">
    Username:<input name="name" type="text" /><br />
    Email:<input name="email" type="text" /><br />
    Phone:<input name="phone" type="text" /><br />
    Password:<input type="password" name="password" /><br />
    <input type="submit" name="submit" value="Register" />
    </form>
    </body>
    </html>

    // it is not working
    // the form is not validated completely..

  • #2
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi All,

    Please reply to my post..Is there any mistakes in that code, I think there is nothing wrong with my code.
    But it is not validating all the text boxes..it just showing 1 alert msg i.e., for username and then it is redirecting to a new form page.

    why it happening like that..help me

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Is this homework of some kind? It works fine for me and shows the alerts for the missing fields. All four fields must be completed before the form submits. But there are quite a few other problems.

    Assigning a name to a form is deprecated (since 1996) and obsolete, and allowed only for the sake of backwards compatibiliy. Use an id instead.

    Alerts are also obsolete and should be used only for testing purposes. Use DOM methods to display a message to your users.

    var name=document.testform.name.value; - It is not a good idea to assign the same name to an HTML element and a Javascript variable. In any case avoid the word name. Prefer username or something. Avoid using any other words which are HTML/JavaScript methods/properties/attributes.

    And *NEVER* use the name/id "submit", because then your JS code can't call document.formname.submit() if you ever need to.

    target="_blank" - What is that supposed to do? That is valid HTML only for a linked document.

    A form field value can never be null - only "" (blank).

    Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe. Numeric values, such as zip codes, phone numbers and dates, should be validated as such. Ditto email addresses. A form which allows a password consisting of a single space is simply silly. This topic has been covered many times before in this forum.

    Finally, be aware that JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation (and even possibly inject malicious code) simply by disabling Javascript.


    BTW, when posting here please help us to help you by making it easier to view, copy, modify, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar.




    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 06-09-2014 at 09:28 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Firstly Thank you Philip.
    Coming to way of posting my questions here, I am new here and from next time onwards will follow the rules.
    And thanks for your reply.
    expect the same in future..and you said right, it is a home work question only. Initially it worked when my sir told it but when I tried the same my system it not working, so I posted it here.

  • #5
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I forgot to ask u one question! You told me to use server side scripting instead of client side but for small form validations using server scripting may increase burden on server or not if form clicks or more.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Quote Originally Posted by PrashanthReddyA View Post
    I forgot to ask u one question! You told me to use server side scripting instead of client side but for small form validations using server scripting may increase burden on server or not if form clicks or more.
    No idea what you mean, but any client-side validation can be by-passed simply by disabling Javascript in the browser. Javascript provides only convenience, not security.

    Please don't get the ideas that we will do your homework for you.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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