0

Can anyone help I'm trying to validate multiple inputs to ensure that they aren't empty at the point I submit. And then add some text into #error.

My form looks like this :

<div class="span12">
        <form method="post" name="bdayInputFormMain" action="/bdayremind_maininput/" class="form-inline">
        {% csrf_token %}
                <input name="name" type="text" class="input-medium" placeholder="Name">
                <input name="dob" type="text" class="input-small datepicker" placeholder="Date of Birth">
                <input name="addr" type="text" class="input-xlarge" placeholder="Address">
                <button type="submit" class="btn">Update</button>
        </form>
</div>

Thanks,

3 Answers 3

1

Check out the documentation for .submit(). It is fairly easy to write your own validation method, or you can use a library like others have suggested.

http://jsfiddle.net/rwFDT/

$('form').submit(function(){
   var $emptyFields = $();
   $('form').find('input').each(
       function(){
              if($(this).val() == ""){
                   $emptyFields.push(this)
             }
        });
  if($emptyFields.length != 0){
    $('form').find('input').css("border","");
    $emptyFields.css("border", "1px solid red");
    $("#error").html("<b><i>This is my error message</i></b>");
    return false;
  }else{
    return true;
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Works well. However if I change the code to slide the error message in, how do I slide the rest of the page down ?
Try some things out, if you are still stuck ask a new question
0

This validation library, Parsley.js, looks pretty nice and may meet your needs.

Comments

0

Yeah! The jquery validation is one of the best validator for client. You should use required class for those field required.

See the doc http://docs.jquery.com/Plugins/Validation

Your form would look like this:

http://jsbin.com/elemad/3/edit

I hope it helps!

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.