SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  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
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're already passing in the element with (this), so there's no need to even reference the form at all.

    Update your checklen function to this

    Code:
    	    function checklen(el) {
    		var length = el.value.length;
    		alert('writwe value');
    		if (length > 10) {
    		    alert('value exceeds 10 characters.');
    		    el.value = '';
    		    el.focus();
    		}
    	    }
    And it should work


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
  •