1

It does not validate before AJAX call to send information to DB. How can i activate the Validation first?

$(document).ready(function() {
        $("#signup").validationEngine('attach', {
            promptPosition : "centerRight",
            scroll : false,
            ajaxFormValidation: true,
            onBeforeAjaxFormValidation: beforeCall,
            onAjaxFormComplete: ajaxValidationCallback,
        });
    });




$("#submit").click(function() {
        var url = "insertintoDB.php"; // the script where you handle the form input.
        $.ajax({
               type: "POST",
               url: url,
               data: $("#signupform").serialize(), // serializes the form's elements.
               success: function(data)
               {
                   alert(data); // show response from the php script.
               }
             });

        return false; // avoid to execute the actual submit of the form.
    });

It does direct to insertintoDB.php but does not validate the following form.

<form class="form-horizontal" id="signup" name="signup" method="post" action="insertDB.php">

 <div class="control-group">
 <label class="control-label" for="inputSurname">Name</label>
 <div class="controls"><input type="text" id="inputSurname" name="inputSurname" placeholder="Surname" class="validate[required]" /></div>
 </div>
<button type="submit" id="submit" class="btn btn-success">Sign Up!</button>

</form>

How can I make it validate first before passing into the PHP for data insertion?

4
  • You should also validate it server-side. Commented Mar 30, 2013 at 11:02
  • Validate on client side helps to reduce load on server Commented Mar 30, 2013 at 11:05
  • you should use jquery forms validation plugins here. Commented Mar 30, 2013 at 11:49
  • ... and you should also learn to give the people that help you a little credit by marking the things the help up or correct Commented Mar 31, 2013 at 13:13

2 Answers 2

1

You can validate your variables using these validation open source libraries:

jQuery-Form-Validator
Lightweight form validation for jQuery or Zepto.js
jQuery-Validation-Engine

And so on ...

But i strongly recommend you use server side validation before do any database action to prevent SQL-injection and XSS attack.

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

Comments

0

Use the jQuery Validator for form validation, with a pattern something like this:

$('form').validate({
    invalidHandler: function(event, validator) {
        // Handle the invalid case.
    },
    submitHandler: function(form) {
       // do stuff on your valid form.
       // Then call form submit.
       form.submit();
    }
});

The validator allows you to set up rules and error messages for various conditions, however it automatically detects missing fields with the 'required' attribute on them and other cases based on type of field. E.g. url - it will require a valid url, etc.

Server side validation is important, but the jQuery validator allows you to catch many things while you still have the users attention and they can easily fix.

See here: jQuery Validator

5 Comments

<script> $(".submit").validate({ invalidHandler: function(event, validator) { // Handle the invalid case. }, function(signupform) { // do stuff on your valid form. // Then call form submit. signupform.submit(); } }); </script>
Sorry, missed something out. That should cover it.
Pardon my ignorance, but what is the replacement for selector? Is that the name of the form?
I am using Jquery Validation Engine
Great then that is the pattern. The selector will be anything that contains the form. So for example if you are using jQuery dialog: something like this: $('form', body).validate()... but really it can be anything. $('form', content).validate...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.