0

i'm on my web site project and it seems that my jquery function doesn't work .i have to validate this:if the user enters <<nom de famille du pere>>,<<prenom du pere>> has to be entered too.This is my html code:

<div class="form-group">
<div class="col-md-6">
  <label class="col-md-4 control-label" for="father">Nom de famille  du père </label>  
  
  <input id="father" name="father" type="text"  class="form-control input-md" >
  </div>   
</div><br/><br/><br/>

<div class="form-group">
<div class="col-md-6">
  <label class="col-md-4 control-label" for="ffather">Prénom du père</label>  
  <input id="ffather" name="ffather" type="text"  class="form-control input-md">
   </div>  
</div><br/><br/><br/>

and this is my jquery function :

$(document).ready(function() {
$('#father').submit(function() {
  if ($(this)).is(':empty')) {
	 $('#ffather').prop('required',false);  
	
	 
}
else{
	$('#ffather').prop('required',true);  
	 
	
}}  }

3
  • 2
    How does one submit an input ? Commented Apr 6, 2015 at 22:04
  • 1
    set the .submit() handler on the form, not the input; or use .change() or .keyup() on the input Commented Apr 6, 2015 at 22:04
  • i forgot to put my form head in the code an d i have one submit button too Commented Apr 6, 2015 at 22:10

2 Answers 2

1

DEMO: http://jsfiddle.net/Lfnrrdfu/1/

you have a few mistakes in your code:

$('#father').on('keyup', function () {
    if (!$(this).val()) {
       $('#ffather').prop('required',false);  
    }
    else{
      $('#ffather').prop('required',true);  
    }
    console.log($('#ffather').prop('required'));
});

You can't use submit event with an input, submit is for a form, you could use the form but then you have to prevent default then check the value of the input.

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

Comments

1

You are trying to set the attributes upon form submission, which is not the right way to do it. You should only check if the fields satisfy the requirements on submit. Defining the constrains should be on the markup. If you are not using any custom form validator, you can use HTML constrain validation. Examples can be found here http://www.w3schools.com/js/js_validation.asp

Again, the jQuery submit event can ONLY be attached to form elements. Review here https://api.jquery.com/submit/

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.