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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    javascript validation starter help

    help me to find error in the below code

    Code:
    <html>
    <head>
    <title></title>
    
    <script>
    var user=document.getElementById('abcd');
    function validate()
    {
    if (newa(user))
    {
    }
    return false;
    }
    
    function newa(user)
    {
    var kks=user.value.length
    if (kks==0)
    {
    document.getElementById('ablab').innerHTML="fdfsdfd";
    return false;
    }
    }
    </script>
    
    
    </head>
    <body>
    <form action="" method="post" onsubmit="return validate()">
    <input id="abcd" type="text">
    <label id="ablab"></label>
    </br>
    <input type="submit">
    </form>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,762
    Thanks
    23
    Thanked 546 Times in 545 Posts
    What are you trying to do?

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    you define the variable "user" before that element exists, so that will always fail.

    either define it within the validate function (which will give it time to get created) or, better, put all of your script after your html, just before the closing /body tag

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    You are going a long way round.

    var kks=user.value.length
    if (kks==0)

    First of all, to assign the meaningless name kks to the length of the variable user is plain silly. Always assign meaningful names/ids. Such as uservaluelen. Likewise assigning an id of abcd for the textbox is also plain daft.

    But there is no point in having an extra function - all you need is

    Code:
    <script type = "text/javascript">
    
    function validate() {
    document.getElementById('message').innerHTML = "";  // reset
    var user = document.getElementById('username').value;
    if (user.length) == 0 {
    document.getElementById('message').innerHTML = "You must enter  your name";
    // re-focus on the input field
    return false;
    }
    }
    
    </script>

    Secondly, form validation of the pattern if (document.formname.formfield.value == "") (or length ==0) - 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. This topic has been covered many times before in this forum.


    And Franco di Santo, who never scored here in his spell with Chelsea, threatened to do the same for the opposition. - Commentator BBC 1
    Last edited by Philip M; 02-23-2013 at 03:49 PM. Reason: Missing "

    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by xelawho View Post
    you define the variable "user" before that element exists, so that will always fail.

    either define it within the validate function (which will give it time to get created) or, better, put all of your script after your html, just before the closing /body tag
    That won't do! Wherever the script is positioned it will attempt to evaluate

    var user=document.getElementById('abcd');

    before the user had had any time to enter his details.

    You must define the variable user within the function.

    But indeed scripts should be positioned in the page right in front of the </body> tag.

    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.

  • #6
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    i am just practising don't suggest me how to write the code.....

    i need your help to find error in above code.the code is not executing

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by leonight View Post
    i am just practising don't suggest me how to write the code.....

    i need your help to find error in above code.the code is not executing
    You have been told very clearly why the code is not executing. And it is not a good reason for writing poor code with silly variable names to say that you are "just practising". As a leaner driver do you deliberately or carelessly drive badly when you are "just practising"?

    If you want to learn anything and "need help" you should pay attention to the advice of experienced people. However:-

    Code:
    var rude = true;
    var thanks = false;
    if (rude && !thanks) {
    var interestLost = true;
    var moreHelp = 0;
    }

    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
    •