0

I have a form containing some values like firstname, lastname, etc. When i am not filling all the details, i have displayed the text please fill all the details and it goes to the next step in jquery . I dont want it to go to the next step.

$("button.info").click(function(event){

         var fnamee = $('input.firstname').val();
         var lnamee = $('input.lastname').val();


         if(fnamee==''||lnamee=='')
        {

             alert("Please Fill All Fields");
             event.preventDefault();

        }
         else{


         }

  });

my form is :

<form>
         <input type="text" class="fname"/>
         <input type="text" class="lname"/>
         <input type="submit" class="info" name="submit"/>
</form>

I have given the preventDefault but it is not working.

Can anyone tell a solution for this ?

4
  • What do you mean with next step? Do you mean the form is submitted, even when not all fields are filled in, or is there some other code on your page that you consider the next step? Commented Jan 21, 2017 at 7:37
  • any console error ? Commented Jan 21, 2017 at 7:38
  • 1
    $("button.info") this is wrong. It should be $(".info") as input is of type submit Commented Jan 21, 2017 at 7:39
  • 1
    @BharatsingParmar, you nailed it. Commented Jan 21, 2017 at 7:41

4 Answers 4

1

Please check your class names. should be .info, .fname and .lname:

$(".info").on('click',function(event){

         var fnamee = $('.fname').val();
         var lnamee = $('.lname').val();


         if(fnamee==''||lnamee=='')
        {

             alert("Please Fill All Fields");
             event.preventDefault();

        }

  });

It is working here: https://jsfiddle.net/Lbx8xh1a/

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

Comments

1

In your code jquery selector is wrong. So preventDefault not working

$("button.info")

This should be

$("input.info").click(function(event){
     var fnamee = $('input.fname').val();
     var lnamee = $('input.lname').val();

    if(fnamee==''||lnamee=='')
    {
         alert("Please Fill All Fields");
         event.preventDefault();
    }
    else{

    }  
});

1 Comment

you changed to fname after my answer :)
1

You have two mistakes in the implementation

  1. Selector: use selector $('input.info') instead of $('button.info')

  2. In the if conditional expression do not check the equality to '' as it can also be undefined. So use if(!fnamee || !lnamee)

And also You can achieve it through multiple ways.

  1. Add required attribute to the input field which by default will prevent the form submission if it is empty.

  2. When the button type is submit, you'll have to return false instead of doing a preventDefault. So i prefer you to write code not on button click but on form submit event. Give form a name such as 'myForm' then

    $('form[name="myForm"]').submit(function() {
         var fnamee = $('input.firstname').val();
         var lnamee = $('input.lastname').val();
             if (fnamee==''||lnamee=='') {
                alert("Please Fill All Fields");
                return false; //it can aslso be preventDefault
             }
    });
    

Hope this helps you!

6 Comments

Why would preventDefault not work? Can you explain?
return false from within a jQuery event handler is same as executing both event.preventDefault and event.stopPropagation on the passed jQuery.Event object. Instead of calling both functions, just use return false;
So why is preventDefault alone not sufficient?
Because if there are any other event delegations attached to form on submit , they will be stopped from bubbling as they're not expected to happen. It is just a good exercise or can be called as safe practice when dealing with form with excessive functionality.
It might be good practice, but it does not explain at all why the OP's code would still have the form submitted.
|
-1

Add an extra class (i.e required) for each required field. Then check each required field by that class using jquery .each() and check whether it is null or not.

For example: HTML

<form>
     <input type="text" class="fname required"/>
     <input type="text" class="lname required"/>
     <input type="submit" class="info" name="submit"/>
</form>

jQuery:

$("button.info").click(function(event){
    var x= true;
    $('.required').each(function(i, obj) {
        if(obj.length == 0)
        {
            x=false;
        }
    });

    if(x){
        //code
    }
    else{
        alert("Please Fill All Fields");
    }
});

2 Comments

Here required not html attr, but a class.
Ah, yes, you're right. So how does this solve the OP's problem. Will it not go to the next step now, and why not?

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.