Selection based validation

A

alexz

I want to check information is entered in the form with javascrip,
based on a radio button or a checkbox. For example I have 2 radio
buttons Yes and No. If "Yes" is click then check if the fname field is
filled. If "No" is checked, check if the lname was filled. Of course i
also need a vlidation for the status (yes or no) it self.
Thank.
 
E

Erwin Moller

alexz said:
I want to check information is entered in the form with javascrip,
based on a radio button or a checkbox. For example I have 2 radio
buttons Yes and No. If "Yes" is click then check if the fname field is
filled. If "No" is checked, check if the lname was filled. Of course i
also need a vlidation for the status (yes or no) it self.
Thank.

Hi,

I would advise you to code that in Javascript.
:p

Regards,
Erwin Moller
 
A

alexz

Erwin Moller said:
Hi,

I would advise you to code that in Javascript.
:p

Regards,
Erwin Moller
Thank Erwin.
You would have an example of that javascript script?
 
E

Erwin Moller

alexz said:
Erwin Moller

Thank Erwin.
You would have an example of that javascript script?

Hi Alex,

I was being just a little ironical.
I did that because I thought your question was very vague.
Being ironical is not nice, so I'll make it up to you.
:)

Two things:
1) Why I think your question is bad.
2) the solution to your problem (assuming I understood well).

1) Why is your question bad?
Because it was not clear what it is you cannot do.
Do you expect this newsgroup to give you some examplecode? Or is there some
particular thing you do not understand?

What is it that you cannot do?
Or what is it that doesn't work?
Do you not know how to get the state of a radiobuttun?
Do you not know how to use an if/else statement?

2) solution.
Because I am in a lot better mood than yesterday, here is the code to check
what you want. :)
I wrote is quit elaborately, to make clear how things work (I hope).

-------------------------------------

<html>
<body>

<script type="text/javascript">
function checkIt(){
var YesChecked = false;
var NoChecked = false;

var theFormRef = document.forms.myForm;

if (theFormRef.myRadioButton[0].checked) {
YesChecked=true;
if (theFormRef.fname.value.length>0) {
theFormRef.submit();
} else {
alert ("You forgot to fill fname!");
}
}
if (theFormRef.myRadioButton[1].checked) {
NoChecked=true;
if (theFormRef.lname.value.length>0) {
theFormRef.submit();
} else {
alert ("You forgot to fill lname!");
}
}

// checked anything?
if ((!YesChecked) && (!NoChecked)){
alert ("please check yes or no!");
}
}

</script>


<form name="myForm" action="somescript.php" Method="post">
<input type="radio" name="myRadioButton" value="Y"> Yes
<br>
<input type="radio" name="myRadioButton" value="N"> No
<br>
fname: <input type="text" name="fname">
<br>
lname: <input type="text" name="lname">

<input type="button" onClick="checkIt()" value="click me">
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top