PDA

View Full Version : help on field validation...



reigalz
Mar 25th, 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 [email protected]#$%^&*()..
thanks in advance ;)

pankaj
Mar 25th, 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
Mar 25th, 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
Mar 25th, 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
Mar 25th, 2003, 06:53 AM
No problem, s will return a value greater than -1 if a bad character ([email protected]#$%^&* 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 [email protected]#$%^&*\(\) characters!";
}
}
</script>

reigalz
Mar 25th, 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 [email protected]#$%^&*\(\) characters!";
}

is it correct?

Mhtml
Mar 25th, 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 [email protected]#$%^&*\(\) characters!");
}

reigalz
Mar 25th, 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 [email protected]#$%^&*\(\) characters!");
a=1;
return false;
}

i'm using onSubmit..

reigalz
Mar 25th, 2003, 07:15 AM
the error is at this line: s = login.username.search(searchFor);
do i need to define s?

Mhtml
Mar 25th, 2003, 07:29 AM
s = login.username.value
s.search(searchFor)

reigalz
Mar 25th, 2003, 07:40 AM
it works already! thanks for ur help! :D

Mhtml
Mar 25th, 2003, 08:00 AM
No problem. :D

Weirdan
Mar 25th, 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
Apr 3rd, 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!