/    Sign up×
Community /Pin to ProfileBookmark

validate check box

Hello world !

I have the following script:

<script type=”text/javascript”>
<!–

String.prototype.isEmailAddress = function () {return /^[^x00-x20()<>@,;:“.[]x7f-xff]+(?:.[^x00-x20()<>@,;:“.[]x7f-xff]+)*@[^x00-x20()<>@,;:“.[]x7f-xff]+(?:.[^x00-x20()<>@,;:“.[]x7f-xff]+)+$/.test(this)}
function validate (f) {
var e, i = 0;
while (e = f[[‘user_name’, ’email’, ‘confemail’, ‘password’, ‘confpassword’][i++]]) {
if (!/S/.test(e.value)) {alert ([‘Field ‘, e.previousSibling.data, ‘is required.’].join (”)); e.focus(); return false}
}
}
// –>
</script>

I need to validate a check box:

<input name=”level” type=”checkbox” id=”level” value=”2″>

How can I make it as a requirement?

Hope for any help

Sandro27

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@7studNov 07.2004 — Validate a checkbox?? ? A checkbox has nothing to validate: it's either checked or not checked. There is no string involved that you would validate.
Copy linkTweet thisAlerts:
@sandro27authorNov 07.2004 — Hi 7 stud,

In this case I am trying to make the check box a requirement. Unless you check it you will get a alert message as " please read the conditions and agree by checking box."

Hope I am clear

Sandro27
Copy linkTweet thisAlerts:
@ExuroNov 07.2004 — You don't need a regexp to see if a checkbox is checked or not, just use the element's [FONT=courier new]checked[/FONT] property:
[code=php]function validate(form) {
if (!form["level"].checked) { // If the box isn't checked
alert("You didn't check the box!");
return false;
}
return true;
}[/code]
Copy linkTweet thisAlerts:
@sandro27authorNov 08.2004 — Thanks a lot Exuro.

That´s solved my problem.

Sandro27
×

Success!

Help @sandro27 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.7,
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,
)...