Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  88] [ 4]  / answers: 1 / hits: 140851  / 11 Years ago, wed, august 28, 2013, 12:00:00

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/>



More From » jquery

 Answers
24

The required property is boolean:



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


Reference: HTMLInputElement


[#76062] Tuesday, August 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayla

Total Points: 681
Total Questions: 102
Total Answers: 108

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
tayla questions
Fri, Mar 5, 21, 00:00, 3 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;