/    Sign up×
Community /Pin to ProfileBookmark

validate a form

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

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@pyroJul 24.2003 — 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>
Copy linkTweet thisAlerts:
@khakiJul 24.2003 — hey pyro...

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

[B]if[COLOR=crimson](/^s*$/.test[/COLOR](frm.mytextarea.value)[/B]

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 [COLOR=crimson].test[/COLOR] part.

thanks...

? k
Copy linkTweet thisAlerts:
@pyroJul 24.2003 — 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
Copy linkTweet thisAlerts:
@DonMArtinauthorJul 24.2003 — thanks a lot, i'll do my very best

Alex
Copy linkTweet thisAlerts:
@khakiJul 24.2003 — 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 [B][COLOR=crimson][SIZE=3]^[/SIZE] [/COLOR][/B] and the [B][COLOR=crimson][SIZE=3]$[/SIZE] [/COLOR][/B]

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

but...

if [B][COLOR=crimson][SIZE=3]s*[/SIZE] [/COLOR][/B] tests for all instances of spaces, tabs, form feeds and line feeds....

then is it necessary (required) to use [B][COLOR=crimson][SIZE=3]^[/SIZE] [/COLOR][/B] and [B][COLOR=crimson][SIZE=3]$[/SIZE] [/COLOR][/B] at all in this case?

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

[B][COLOR=crimson][SIZE=3]/s*/[/SIZE] [/COLOR][/B]

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
Copy linkTweet thisAlerts:
@pyroJul 24.2003 — 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... ?
Copy linkTweet thisAlerts:
@khakiJul 24.2003 — oh

right

DUH :rolleyes:

yikes... that was embarrassing ?

sorry to put you through that

jelly-brain....

? k
Copy linkTweet thisAlerts:
@pyroJul 24.2003 — lol. ?

No problem... Glad to help.

Cheers!
×

Success!

Help @DonMArtin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...