Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adapt character validation function to also check length of field.

Status
Not open for further replies.

uncleroydee

Technical User
Nov 28, 2000
79
US
I would like to adapt the following function, which I am already using, to also ensure the length of the field as 13 characters. The function currently checks that a field contains only numeric data and is called by onblur event handlers in the individual input fields.

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

<!-- Begin
function validate(field) {
var valid = &quot;0123456789&quot;
var length = 13
var ok = &quot;yes&quot;;
var temp;
for (var i=0; i<field.value.length; i++) {
temp = &quot;&quot; + field.value.substring(i, i+1);
if (valid.indexOf(temp) == &quot;-1&quot;) ok = &quot;no&quot;;
}
if (ok == &quot;no&quot;) {
alert(&quot;Invalid entry! NSNs can only contain integers and must be 13 digits.&quot;);
field.focus();
field.select();
}
}
// End -->
</script>

Thanks.
 
Will the following not work??

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

<!-- Begin
function validate(field) {
var valid = &quot;0123456789&quot;
var length = 13
var ok = &quot;yes&quot;;
var temp;
for (var i=0; i<field.value.length; i++) {
temp = &quot;&quot; + field.value.substring(i, i+1);
if (valid.indexOf(temp) == &quot;-1&quot;) ok = &quot;no&quot;;
}
if (field.value.length != 13) {ok = &quot;no&quot;;}

if (ok == &quot;no&quot;) {
alert(&quot;Invalid entry! NSNs can only contain integers and must be 13 digits.&quot;);
field.focus();
field.select();
}
}
// End -->
</script>
Mise Le Meas,

Mighty :)
 
It works well. Someone else sent me this

if(parseFloat(fieldLength)<13)
{
ok = &quot;no&quot;;
}

which also works.

Thanks for the response.

Roy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top