...

View Full Version : Form field length



pwsparrow
10-14-2005, 09:52 PM
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()"


<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>

Brandoe85
10-14-2005, 10:12 PM
If you only want to allow 6 characters, you can just check if it's not equal to 6:


<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

pwsparrow
10-14-2005, 10:18 PM
Is the problem that onBlur is the wrong call? Should I use onChange or something, its just not responding.

Brandoe85
10-14-2005, 10:38 PM
No, onblur is fine. Post your entire code, everything seems to be working for me.

pwsparrow
10-14-2005, 10:42 PM
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.


<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>

Pyth007
10-14-2005, 11:45 PM
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:


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".

Philip M
10-15-2005, 09:29 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum