0

I am creating a simple form that contains text fields.

These text fields are saved on change events using Ajax.

My question is i want to validate(jquery) these fields before change event.

Please, help. Thanks....... :)

5 Answers 5

1
$('input').change(function(){
  vat retrn = validatethisfield();
if(retrn == false)
{
//show msg;
return false;
}

})

In this validate this field function , do your validation and return false if invalid data...

0

Do validation on blur or focusout

0

There are many ways of doing this, thou plugins are readily available,for start you can learn you can write your code which is very easy to learn one of link you can refer is this one....

It covers almost all aspect of form validation:

link

0

Or on your submit button , use validation function.

<input type="submit" value= "submit it" onclick= "javascript:return formvalidator()">

always return true unless a text box is empty or something of that nature

function formvalidator(){
var test = true;

if($trim.$("#myTextField").val()) == " "){
alert('Empty!!');
test = false;
}

return test;
}
2
  • I dont have submit button. I saving fields on change event of text fields using ajax call.
    – Sachin J
    Mar 12, 2012 at 13:46
  • then put the content of my formvalidator inside your change event
    – Har
    Mar 12, 2012 at 13:57
0

jQuery.ajax has a beforeSend. You can do your validations in this function.

$.ajax({
  url: '/some/path',
  beforeSend: function( xhr ) {
    // do your validations here
  },
  success: function( data ) {
    // handle success
  }
});

To quote jquery's documentation:

beforeSend(jqXHR, settings) - A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object before it is sent. Use this to set custom headers, etc. The jqXHR and settings maps are passed as arguments. This is an Ajax Event. Returning false in the beforeSend function will cancel the request. As of jQuery 1.5, the beforeSend option will be called regardless of the type of request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.