Forum Moderators: open

Message Too Old, No Replies

contains valid characters

A-Za-z0-9 and space

         

WhosAWhata

6:26 am on Jun 10, 2004 (gmt 0)

10+ Year Member



If I have a text field <form name="myform"><input type="text" name="mytext">

how can i have <input type="submit" value="submit" onclick="validate()"> where validate is a function that will make sure document.myform.mytext.value contains only letters (upper and lower case), numbers, and spaces and return false if not?
in php
<?
function validate($text){
if(ereg("^[A-Za-z0-9 ]+$",$text)) {
return true;
}else {
return false;
}
}
?>

i think i got that syntax right...thanks

Bernard Marx

9:45 am on Jun 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What the question, WhosAWhata? I think this will help:

<input type="submit" value="submit" onclick="validate([blue]this.form.mytext.value[/blue])">

then

function validate(value){
if(/^[A-Za-z0-9 ]+$/.test(value)) {
return true;
}else {
return false;
}
}

The real suggestion I'm making is to send the textarea's value with the

[blue]validate()[/blue]
call.
I'm assuming that your regexp is correct. I'm fairly sure that the form I've turned it into will be OK, and save a function call
[blue]ereg[/blue]
inside.

WhosAWhata

5:39 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



<input type="submit" value="submit" onclick="validate(this.form.mytext.value);validate(this.form.mytext2.value)">

will this work?