View Full Version : help on field validation...
reigalz
03-25-2003, 05:58 AM
hi all....how do i validate a field whereby the users can only enter characters and numbers only?? which means they can't enter invalid characters like !@#$%^&*()..
thanks in advance ;)
pankaj
03-25-2003, 06:22 AM
In Front Page if u select the text box and double click on it , u will see a window which has a validate button. select the appropriate options. This will generate a JScript function.
I suggest u go thru the function and change it according to ur needs.
Mhtml
03-25-2003, 06:40 AM
Front page.. eeewwww.
<script>
function validate(theForm,theField){
string = theForm.theField;
var searchFor = "[^A-Za-z0-9_]";
s= string.search(searchFor);
return s;
}
</script>
reigalz
03-25-2003, 06:47 AM
hi Mhtml, wat will s return? when will it return true?
can u explain abit on the code? thanks alot!
Mhtml
03-25-2003, 06:53 AM
No problem, s will return a value greater than -1 if a bad character (!@#$%^&* etc..) is found, if it is all clean of the bad characters it will return -1..
You can change it like this to give an alert!
<script>
function validate(theForm,theField){
string = theForm.theField;
var searchFor = "[^A-Za-z0-9_]";
s= string.search(searchFor);
if (s == -1){
theForm.submit
}else{
alert("You have entered invalid characters!\n Please avoid using !@#$%^&*\(\) characters!";
}
}
</script>
reigalz
03-25-2003, 07:05 AM
i tried, but there's an error....it says: Object doesn't support this property or method. Why is it so?
my form name is login and my field name is username so my code should be:
string = login.name;
var searchFor = "[^A-Za-z0-9_]";
s= string.search(searchFor);
if (s == -1){
login.submit
}else{
alert("You have entered invalid characters!\n Please avoid using !@#$%^&*\(\) characters!";
}
is it correct?
Mhtml
03-25-2003, 07:09 AM
string = login.username.value;
var searchFor = "[^A-Za-z0-9_]";
s= string.search(searchFor);
if (s == -1){
login.submit()
}else{
alert("You have entered invalid characters!\n Please avoid using !@#$%^&*\(\) characters!");
}
reigalz
03-25-2003, 07:14 AM
there's still the same error...
here's my code:
if(username.length>0 && a==0) {
string = login.username.value;
var searchFor = "[^A-Za-z0-9_]";
s = login.username.search(searchFor);
if(s==-1) {
return true;
} else {
window.alert("You have entered invalid characters!\n Please avoid using !@#$%^&*\(\) characters!");
a=1;
return false;
}
i'm using onSubmit..
reigalz
03-25-2003, 07:15 AM
the error is at this line: s = login.username.search(searchFor);
do i need to define s?
Mhtml
03-25-2003, 07:29 AM
s = login.username.value
s.search(searchFor)
reigalz
03-25-2003, 07:40 AM
it works already! thanks for ur help! :D
Mhtml
03-25-2003, 08:00 AM
No problem. :D
Weirdan
03-25-2003, 10:33 AM
You'd better use character classes for validation:
var searchFor = "[\w]"
At present time it just equivalent to [A-Za-z0-9_] but someday ECMA will introduce locale-dependent regexp matching and Microsoft will implement it, I hope :)
reigalz
04-03-2003, 01:16 PM
string = login.username.value;
var searchFor = "[^A-Za-z0-9_]";
s= string.search(searchFor);
the above code search for invalid characters...it also includes space....is there any code which only search for invalid characters except space....anyone noes? thanks in advance!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.