Click to See Complete Forum and Search --> : validate a form


DonMArtin
07-24-2003, 07:13 AM
Hi Pros,

I want to validate a form with 2 Radios and 1 textfield. The textfield should only be validated if the 2nd radio is checked.

I don't want to do it in php.

Thenks Alex

pyro
07-24-2003, 08:44 AM
Try this out:

<html>
<head>
<script type="text/javascript">
function validate(frm) {
if (frm.myradio[1].checked) {
if(/^\s*$/.test(frm.mytextarea.value)) {
alert ("Please fill out the textarea");
return false;
}
else {
return true;
}
}
}
</script>
</head>

<body>
<form name="myform" onsubmit="return validate(this);">
<input type="radio" name="myradio" value="0">
<input type="radio" name="myradio" value="1">
<textarea name="mytextarea"></textarea>
<input type="submit" value="submit">
</form>
</body>
</html>

khaki
07-24-2003, 09:14 AM
hey pyro...

any chance that you could explain the parts of this part:

if(/^\s*$/.test(frm.mytextarea.value)

I assume that this just checks the field to see if it is empty or contains just a space...
but I don't understand the use of the symbols and especially the .test part.

thanks...
;) k

pyro
07-24-2003, 09:24 AM
Sure thing.

It is a regular expression that tests a field to check if it is blank, or only contains spaces. \s matches a single white space character, including space, tab, form feed and line feed. The * tells it to match it 0 or more times. The .test part tells it what you are testing (in my example, we are testing the form field 'mytextarea'). Had we been testing a variable, it would have been .test(varname).

More info on regexp can be found at http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/regexp.html#1193136

DonMArtin
07-24-2003, 09:30 AM
thanks a lot, i'll do my very best

Alex

khaki
07-24-2003, 09:57 AM
thanks pyro....
that's a great link :)

but it also causes me to ask some more questions :rolleyes:

I'm going to make some assumptions....
so please correct me if I am wrong about them.

I assume that the forward slashes are used as a "container" for the expression (?)

But what I am having difficulty with is the ^ and the $

I understand that the first tests the beginning of the input, and the second one tests the end of the input.

but...
if \s* tests for all instances of spaces, tabs, form feeds and line feeds....
then is it necessary (required) to use ^ and $ at all in this case?

I guess I'm asking if this would work just as effectively if it was just written like this:
/\s*/

Does that make sense?

I can't believe it took me this long to learn what all of that stuff meant...
I'm glad I finally asked....
and thanks for answering.

;) k

pyro
07-24-2003, 10:22 AM
Yes, everything between the / / are regexp expressions.

/\s*/ howerver, is not the same as /^\s*$/. If you were to use /\s*/, the if statement would return true if it encounters any spaces. ie. 'testing this' would return true (and thus alert the users, and return false on the form, causing it not to submit).

If we use /^\s*$/, we are checking the field like this:

If the form starts with a space, contains only spaces, and ends with a space, alert the user.

If we use /\s*/, we are checking the field like this:

If the form contains any spaces, alert the user

Hope that helps... :)

khaki
07-24-2003, 10:26 AM
oh
right
DUH :rolleyes:

yikes... that was embarrassing :(

sorry to put you through that

jelly-brain....
;) k

pyro
07-24-2003, 10:30 AM
lol. :D

No problem... Glad to help.

Cheers!