SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy javascript validation

    I am doing length validation but it's not working. For this I am using onBlur event but it is not working. Please help me how to do it. I think it is not firing onBlur event.

    My code is following -

    <body>
    <script language="javascript1.2">
    function verify(form)
    {
    if(form.my.value=="")
    {
    alert("please fill");
    document.form.my.focus();
    return(false);
    }
    }

    function checklen(form)
    {
    val = form.my.value;
    len=val.length;
    alert("writwe value");
    if(len>10)
    {
    alert("value exceeds 10 character");
    form.my.value="";
    form.my.focus();
    }
    }


    </script>
    <form name="form" method="post" onSubmit="return verify(this)" action="validation2.php">
    <p>
    <input type="text" name="my" onBlur="checklen(this)">
    </p>
    <p>
    <input type="text" name="my2">
    </p>
    <p> <input type="submit" name="Submit" value="Submit" >
    </p>
    </form>
    </body>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the onsubmit handler for your form, you pass the parameter "this", which correctly passes a reference to the form.

    However you are passing "this" in the onBlur handler for the input. This passes a reference to the input field, not the form.

    I would suggest changing
    onBlur="checklen(this);"
    to
    onblur="checklen(this.form);"

    to get a proper reference to the form - or rewrite the checklen function to expect an input as a parameter:
    Code:
    function checklen(input)
    {
    val = input.value;
    len=val.length;
    alert("writwe value");
    if(len>10)
    {
    alert("value exceeds 10 character");
    input.value="";
    input.focus();
    }
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. It is working now.


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
  •