1

I have radio button input, text field input and password field input and one submit button. What I want to happen is, if I click the submit button without choosing or entering those three form, window alert will show saying "You need to fill up the (radio button, text field and password field)" something like that. How can I do that?

This is my code

<form>
  <input type="radio" name="optradio">Menu 1
  <input type="radio" name="optradio">Menu 2 <br>
  <input type="text"><br>
  <input type="password"><br>
  <input type="submit" value="Submit" onclick="alert('Hello! I am an alert box!')">
</form>

4
  • you need to write JavaScript function to validate input fields Commented Oct 17, 2019 at 4:41
  • Where's your attempt at the JavaScript code? Commented Oct 17, 2019 at 4:41
  • better remove the question try yourself first , research yourself then post it again with your attempt. Else unnecessarily it will be down-voted and removed. Commented Oct 17, 2019 at 4:43
  • Use jQuery Validations. Commented Oct 17, 2019 at 4:52

3 Answers 3

3

You can try the using required attribute

<form>
  <input type="radio" name="optradio" required>Menu 1
  <input type="radio" name="optradio">Menu 2 <br>
  <input type="text" required><br>
  <input type="password" required><br>
  <input type="submit" value="Submit">
</form>

Sign up to request clarification or add additional context in comments.

Comments

0

You can have your own function and you can check whether value exist or not. Example:

function myFun(){
    if(val1&&val2&&val3){
    }
}

Comments

0

You can validate this using Javascript

function validation(){
  radio_1_value = document.getElementById("radio_1").value;
  radio_2_value = document.getElementById("radio_2").value;
  text_value = document.getElementById("text").value;
  password_value = document.getElementById("password").value;
  if (radio_1_value == '' || radio_2_value == '' || text_value == '' || password_value == ''){
    alert('You need to fill up the (radio button, text field and password field');
  }
}
<form>
  <input type="radio" id="radio_1" name="optradio" required>Menu 1
  <input type="radio" id="radio_2" name="optradio">Menu 2 <br>
  <input type="text" id="text" required><br>
  <input type="password" id="password" required><br>
  <input type="submit" value="Submit" onclick="validation()">
</form>

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.