...

View Full Version : Client side validation



gsanbrook
07-12-2004, 05:43 PM
Hello all,
I have spent the better part of the morning trying to write a entry field form validation. I want the user to be able to enter a-z (upper/lower fine), underscore, 0-9, spaces and nothing else.

Can someone help?

Thanks,
Gsanbrook

Basscyst
07-12-2004, 06:12 PM
Here you go should do the trick.



<html>
<head>
<script>
function formValid(frm)
{
var chk=frm.value.search(/[^A-Za-z0-9_\s]/); //allow only A-Z,a-z,_ & 0-9 space
if(chk==-1)
{
alert('OK')
}
else
{
alert('No Good');
}
}
</script>
</head>
<body>
<form>
<input type="text" onblur="formValid(this);">
</form>
</body>
</html>


Basscyst

Oops still learning this, should work better now. :o

Choopernickel
07-12-2004, 06:24 PM
Or, for a less interrupted user experience, this:
<html>
<head>
<script type="text/javascript">
function allowWordChars (field) {
if (field.value.match(/\W/)) {
return false;
}
return true;
}
</script>
</head>
<body>
<form>
<input type="text" onkeydown="return allowWordChars(this);" />
</form>
</html>

edit: missing parin

Willy Duitt
07-12-2004, 06:28 PM
Or for those whom need their hand held.......



<input type="text"
onkeyup="if(!this.value.charAt(this.value.length-1).match(/\w|_|\s/gi)){
alert('illegal character');this.value=this.value.substring(0,this.value.length-1)}">


.....Willy

gsanbrook
07-12-2004, 07:24 PM
Thanks to all above, problem resolved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum