/    Sign up×
Community /Pin to ProfileBookmark

Form validating script recommendation

Hi all,

Can anyone recommend me a short and useful script which I can validate text boxes in my forms. I have a 50 textboxes/textareas on my form. Some needs to be entered with only 0-9, some needs only a-z A-Z characters.

What I am looking for is something like, after the main javascript code adding one line for each textbox/textarea which stops the client enter nothing different from the defined characters.

I will be glad if you help.

Thanks

Ceyhun

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMay 17.2006 — Form field [b]value[/b] properties are always string values. So, you can add additional string validation methods such as the following -- which use [I]Regular Expression[/I] evaluation methods:
String.prototype.isEmpty = function() {
return /^s*$/.test(this.toString());
}
String.prototype.isAlpha = function(max, min) {
if(typeof max == "number") {
if(typeof min != "number") min = 1;
var re = new RegExp("^[a-z]{"+min+","+max+"}$", "i");
} else {
var re = new RegExp("^[a-z]+$", "i");
}
return re.test(this.toString());
}
String.prototype.isDigits = function(max, min) {
if(typeof max == "number") {
if(typeof min != "number") min = 1;
var re = new RegExp("^[0-9]{"+min+","+max+"}$");
} else {
var re = new RegExp("^[0-9]+$");
}
return re.test(this.toString());
}

etc...
Copy linkTweet thisAlerts:
@telmessosauthorMay 17.2006 — sorry it looked a little bit complicated to me. what will I add to the form fields????

thanks
Copy linkTweet thisAlerts:
@phpnoviceMay 17.2006 — Examples:

To require a text field have something (anything) in it:

if([I]fieldObj[/I].value.isEmpty()) {

alert("Field is required.");

[I]fieldObj[/I].select();

[I]fieldObj[/I].focus();

return false;

}

To require a text field have 4 to 8 alphabetic characters in it:

if([I]fieldObj[/I].value.isEmpty()

||![I]fieldObj[/I].value.isAlpha(8,4)) {

alert("Field is required to have 4 to 8 alphabetic characters.");

[I]fieldObj[/I].select();

[I]fieldObj[/I].focus();

return false;

}

To make text field optional, but if entered, must have 4 to 8 alphabetic characters :

if(![I]fieldObj[/I].value.isEmpty()

&&![I]fieldObj[/I].value.isAlpha(8,4)) {

alert("If entered, Field must have 4 to 8 alphabetic characters.");

[I]fieldObj[/I].select();

[I]fieldObj[/I].focus();

return false;

}

etc. Need something specific?
Copy linkTweet thisAlerts:
@telmessosauthorMay 17.2006 — I have 14 fields to be limited to 0-9 and 12 fields to be limited to a-z / A-Z
Copy linkTweet thisAlerts:
@phpnoviceMay 17.2006 — To make text field optional, but if entered, must have all numeric digits:

if(![I]fieldObj[/I].value.isEmpty()

&&![I]fieldObj[/I].value.isDigits()) {

alert("If entered, Field must have all numeric digits.");

[I]fieldObj[/I].select();

[I]fieldObj[/I].focus();

return false;

}

To make text field required and must have all numeric digits:

if([I]fieldObj[/I].value.isEmpty()

||![I]fieldObj[/I].value.isDigits()) {

alert("Field is required and must have all numeric digits.");

[I]fieldObj[/I].select();

[I]fieldObj[/I].focus();

return false;

}
Copy linkTweet thisAlerts:
@telmessosauthorJul 17.2006 — Hi,

I am not really good with Javascript so I don't know how to call this function. Can you help???

Thanks

Ceyhun
×

Success!

Help @telmessos 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,
)...