-2

I have one situation where i want detect that user has input some value or not.Let me explain :

I have one registration form where i have user name, email, phone, password this field are there. Now in that form I have two buttons "Save", "Continue".

When user click on "Save" it will fire validation. When click on "Continue" user will have a popup that "Are you sure you do not want to fill the form?" , here without validation fire i would like to user redirect to another page.

How could I overcome this issue? Is there any event that can help me to find out that user did not entered any thing in form? Or any jQuery?

Thanks

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
Hitesh S
  • 460
  • 1
  • 5
  • 20
  • some simple if's will do a fine job – madalinivascu Sep 06 '16 at 04:44
  • Take a look http://stackoverflow.com/questions/3937513/javascript-validation-for-empty-input-field – aavrug Sep 06 '16 at 04:46
  • if's will create lots of efforts in future. Is there any simplest way ? Sometimes it may happen user has inputted in some fields. I want to catch that event that user view that form but not filled any value. – Hitesh S Sep 06 '16 at 04:57
  • _"Is there any simplest way"_ , _"I want to catch that event that user view that form but not filled any value"_ Have you tried approach at Answer using `required` attribute? – guest271314 Sep 06 '16 at 05:00
  • @guest271314 yes i tried "required" but it shows me required alert message. That i don't want to show to user. – Hitesh S Sep 06 '16 at 05:23
  • @Meteor Requirement is to not show any message? – guest271314 Sep 06 '16 at 05:27
  • @guest271314 yes. "When click on "Continue" user will have a popup that "Are you sure you do not want to fill the form?" , here without validation fire i would like to user redirect to another page." – Hitesh S Sep 06 '16 at 05:28
  • @Meteor See updated post – guest271314 Sep 06 '16 at 05:37

2 Answers2

0

Have a manual function which tests for form fields when you press the Continue button.

function isFilled() {

   if ($('#myFields1').value !== ''  && $('#myField2').value !== '') 
      return true
   else
      return false

}
Charlie
  • 22,886
  • 11
  • 59
  • 90
-1

You can use required attribute at form elements. form will not be valid if field having required attribute without value is present. You can also use pattern attribute to specify RegExp pattern for valid input.

<form>
  <input type="text" />
  <input type="text" />
  <input type="text" />
  <input type="submit">
</form>
<script>
  document.querySelector("form").onsubmit = function(e) {
  e.preventDefault();
  for (input of e.target.querySelectorAll("input")) {
    if (input.value.length === 0) {
      // redirect here
      location.reload();
      break;     
    }
  }
  // else submit form at complete of loop
  this.submit();
}
  </script>
guest271314
  • 1
  • 15
  • 104
  • 177