1

Below is my code

<!DOCTYPE html>
<head>

</head>

<body>
<form action="script.php" method="post" id="Form1">
<input name="radioGroup" type="radio" value="Radio1" id="Radio1id">
<input name="radioGroup" type="radio" value="Radio2" id="Radio2id">
<input name="radioGroup" type="radio" value="Radio3" id="Radio3id">
<br>
<br>
<input type="text" size="30" id="name" placeholder="Name*"><br>
<input type="text" size="30" id="email" placeholder="Email*"><br>
<input type="text" size="30" id="comments" placeholder="Comments (Optional)"><br><br>
<input type="submit">
</form>
</body>
</html>

Is there any way to validate radio button and text filed at same time. for an example if radio button is not checked or text field not used. should get a alert window.

Thanks,

3
  • Use jQuery validator for robust validation support. Commented Jan 25, 2017 at 11:12
  • Have you tried validating with Javascript. This question will get downvoted as it has been answered numerous times. Please add the JS code that you've tried so far. Commented Jan 25, 2017 at 11:13
  • Your can use the "required" attribute to make the input required, before the form is submitted. Just add required="true" to the fields you want to make mandatory. Commented Jan 25, 2017 at 11:14

3 Answers 3

3

Try something like that, or use JQuery because is easy to use for validation

if((document.getElementById('Radio1id').checked) && document.getElementById('name').value != "") {
 //Action for checked radio button and text box without value
}else if(document.getElementById('Radio2id').checked) {
 //Another Action . . . 
}

if((document.getElementById('Radio1id').checked == false) && document.getElementById('name').value != "") {
 //Action for NON checked radio button and text box without value
}
Sign up to request clarification or add additional context in comments.

1 Comment

In JS use for logical operator AND -> &&, I write it directly from head, but logic is ok, sorry for that
0

by using jQuery.

 // valid syntax for jQuery
 $("#Radio1id").is(":checked"); // for radio button
 $("#email").val()===""; // for any input

Comments

0

use jQuery Validator , gives you more feature and choices down the road with any project. Plus, you can ask questions on GitHub.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.