Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if an input field is required using jQuery

What would be the easiest way to check if an input is required? I have been trying stuff along these lines but always comes up with all required (only 4/6 are).

$('form#register').find('input').each(function(){     if($(this).prop('required') == 'undefined'){         console.log("NR");     } else {         console.log("IR");     } }) 

(I have tried .attr aswell)

Im trying to use it for form ajax validation, Im doing it this way at the moment:

if($('form#register span').length == $('form#register').children(".green").length){     $('input#register-sub').prop('disabled', false); } else {     $('input#register-sub').prop('disabled', true); } 

Thanks.

EDIT: Html adding

<form id="register" action="" method="post" autocomplete="on"> <label>Nickname:</label><input type="text" name="name" value="<? echo $_POST['name'] ?>" required="" /><span id="reg-name"></span><br />  <?  if($user->type == "l"){ ?> <label>email:</label><input type="email" name="email" value="<? echo $_POST['email'] ?>" required="" /><span id="reg-email"></span><br /> <label>Password:</label><input type="password" name="password" value="<? echo $_POST['password'] ?>" required="" /><span id="reg-password"></span><br /> <label>Again:</label><input type="password" name="password-test" value="<? echo $_POST['password-test'] ?>" required="" /><span id="reg-password-test"></span><br /> <label>Avatar:</label><input type="url" name="avatar" value="<? echo $_POST['avatar'] ?>" /><span id="reg-avatar"></span><br />  <? } ?>  <input type="submit" value="Register" disabled="" id="register-sub"/> 

like image 663
Tristan Cunningham Avatar asked Aug 28 '13 18:08

Tristan Cunningham


People also ask

How do you know if input is required?

Use the required property to check if an element is required, e.g. if (element. required) {} . The required property returns true when the element is required, otherwise false is returned. Here is the HTML for the examples in this article.

How check input field is empty or not in jQuery?

To check if the input text box is empty using jQuery, you can use the . val() method. It returns the value of a form element and undefined on an empty collection.

How can we make text field mandatory in jQuery?

$("input"). attr("required", "true");

How to check if inputs are empty in jQuery?

You can use the val() method to test or check if inputs are empty in jQuery. The following example will add a red outline around the inputs if it is focused but not filled out.

How to validate numeric input in JavaScript?

JavaScript is often used to validate numeric input: Automatic HTML Form Validation HTML form validation can be performed automatically by the browser: If a form field (fname) is empty, the required attribute prevents this form from being submitted:

How to get only the required input fields in a document?

Here's an ES2015 solution: // Get all input fields const inputs = document.querySelectorAll ('#register input'); // Get only the required ones const requiredFields = Array.from (inputs).filter (input => input.required); // Do your stuff with the required fields requiredFields.forEach (field => /* do what you want */);

How do I check if a form is valid in JavaScript?

JavaScript Form Validation HTML form validation can be done by JavaScript. If a form field (fname) is empty, this function alerts a message, and returns false, to prevent the form from being submitted:


2 Answers

The required property is boolean:

$('form#register').find('input').each(function(){     if(!$(this).prop('required')){         console.log("NR");     } else {         console.log("IR");     } }); 

Reference: HTMLInputElement

like image 134
rink.attendant.6 Avatar answered Sep 21 '22 05:09

rink.attendant.6


A little bit of a more complete answer, inspired by the accepted answer:

$( '#form_id' ).submit( function( event ) {         event.preventDefault();          //validate fields         var fail = false;         var fail_log = '';         var name;         $( '#form_id' ).find( 'select, textarea, input' ).each(function(){             if( ! $( this ).prop( 'required' )){              } else {                 if ( ! $( this ).val() ) {                     fail = true;                     name = $( this ).attr( 'name' );                     fail_log += name + " is required \n";                 }              }         });          //submit if fail never got set to true         if ( ! fail ) {             //process form here.         } else {             alert( fail_log );         }  }); 

In this case we loop all types of inputs and if they are required, we check if they have a value, and if not, a notice that they are required is added to the alert that will run.

Note that this, example assumes the form will be proceed inside the positive conditional via AJAX or similar. If you are submitting via traditional methods, move the second line, event.preventDefault(); to inside the negative conditional.

like image 36
JPollock Avatar answered Sep 19 '22 05:09

JPollock