/    Sign up×
Community /Pin to ProfileBookmark

Another Form Validation Question…

How would I go about validating this field so that it only accept exactly 12 numbers

if (document.form.idnum.value==””) {
window.alert (“Please enter your ID Number!”);
document.form.idnum.focus();
return false;
}

if (isNaN(document.form.idnum.value)) {
window.alert (“Your ID Number Must be Numerical and contain 12 Digits!”);
document.form.idnum.focus();
return false;
}

hte form validates ok at present for a null value and a numerical value but i would like it to validate for 12 only as well… TIA

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Mar 31.2003 — you can use an html elements attribute.

input tag has an attribute "maxlength"

you can set it in the text fields html declaration.

For e.g in this case

<input type="text" name ="idnum" maxlength="12"> will take care of that you won't need javascript to validate that.

Cheers

Khalid
Copy linkTweet thisAlerts:
@ozzauthorMar 31.2003 — But what happens if someone only types in 10 numbers? The idnum must be exactly 12.
Copy linkTweet thisAlerts:
@khalidali63Mar 31.2003 — in that case you can add to this line

if (isNaN(document.form.idnum.value)) {

as

if (isNaN(document.form.idnum.value) && document.form.idnum.value.length<12

and so on
×

Success!

Help @ozz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...