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 Coder
    Join Date
    Jul 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form field length

    Hi, im having some problems with limiting the entries to 6 digits only. Here is my code, am i just missing something big here? Its supposed to check it as soon as it tabs to the next field.

    onBlur="return Length_TextField_Validator()"

    Code:
    <script>
    
    function Length_TextField_Validator()
    {
    if ((form_name.text_name.value.length < 6) || (form_name.text_name.value.length > 6))
    {
    mesg = "You have entered " + form_name.text_name.value.length + " character(s)\n"
    mesg = mesg + "Valid entries are 6 characters.\n"
    mesg = mesg + "Please verify your input and submit again."
    alert(mesg);
    form_name.text_name.focus();
    return (false);
    }
    return (true);
    }
    </script>

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    If you only want to allow 6 characters, you can just check if it's not equal to 6:
    Code:
    <script type="text/javascript">
    function Length_TextField_Validator()
    {
    	if (form_name.text_name.value.length != 6)
    	{
    		mesg = "You have entered " + form_name.text_name.value.length + " character(s)\n"
    		mesg += "Valid entries are 6 characters.\n"
    		mesg += "Please verify your input and submit again."
    		alert(mesg);
    		form_name.text_name.focus();
    		return false;
    	}
    
    	return true;
    }
    </script>
    Good luck

  • #3
    New Coder
    Join Date
    Jul 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is the problem that onBlur is the wrong call? Should I use onChange or something, its just not responding.

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    No, onblur is fine. Post your entire code, everything seems to be working for me.

  • #5
    New Coder
    Join Date
    Jul 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The whole script is way too long, about 600 lines, most of which are not relevant i think. The script you edited is between the head obviously, and here is the <td> in which the textbox is in.

    I have nother JS in there that enables enter to work as a tab, and to validate that only integers exist.

    Code:
    <td class="mainlink" valign="middle" width="121"><span class="mainlink">Date:</span></td>
    														<td valign="middle" width="161">
    														<input class="mainlinkblack" type="text" name="txtDate" size="4" maxlength="6" onkeypress="return handleEnter(this, event)" tabindex="7" onChange="intOnly(this);" onKeyUp="intOnly(this);" onKeyPress="intOnly(this);" onBlur="return Length_TextField_Validator()"> <span class="mainlinkred">(MMDDYY)</span>
    														 </td>

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What do you mean by "not responding"? Do you even get the alert() message? Or is it just that it's not focusing on the element again?

    You may need to direct which element a little more (eg document.form.element.value.length). Or you may try DOM methods instead: "document.getElementById('txtDate').value.length" In fact, as a little clean-up I'd have:
    Code:
    var txtElement = document.getElementById('txtDate');
    if (txtElement.value.length!=6)...
    txtElement.focus(); txtElement.select();
    I'd also go with the "!=" instead of two separate conditions as well as mesg+="next line" instead of mesg=mesg + "next line".

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    pwsparrow wanted 6 digits, not 6 characters.

    So best to use a regular expression:-

    if (/^\d{6}$/.test(txtElement.value)) { .... // tests for 6 digits and nothing but. If a match found value passes.

    else ...... / not a valid input value


  •  

    Posting Permissions

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