2

I use LiveValidation plugin (http://livevalidation.com). I need validate one input and if it is ok, make same ajax. Instead submit button I have img and I would like to process it by onClick action. Everything works validation works live, but when I click on image I need to check if is email fill and valid. Is some method like isValid or how can I do it and avoid to process ajax always after click?

<input id="email" type="text" name="email" />
<img id="sendToEmail" src="send.png" />

<script type="text/javascript">
var email = new LiveValidation('email', { validMessage: ' ', wait: 500 } );
email.add( Validate.Presence, { failureMessage: "Is required" } );
email.add( Validate.Email, { failureMessage: "Must by email" } );

$('#sendToEmail').click(function() {
  if (email.isvalid) {
    //ajax calling
  }
})
</script>

This is right solution

if (email.validate()) {
  // some process if is validated
}
tomasr
  • 485
  • 1
  • 8
  • 24
  • see this answer - frontend validation for email what you can use to check before you allow any follow actions: http://stackoverflow.com/q/5778844/532102 – tmaximini Nov 22 '11 at 14:17

1 Answers1

0

You can use:

 $(function(){
    $('#sendToEmail').click(function(){
        if ( !email.validationFailed ){
           //ajax calling
        }
    });
});
Tim B James
  • 20,084
  • 4
  • 73
  • 103
  • But I use that **plugin**, which validate live and I would like to do whith that. – tomasr Nov 22 '11 at 13:26
  • I use that normally, but in this specific case I want submit by click on image. Action which would follow I would allow when input is validated. But I don't now how I find out that. It's not anything like **isValid** function there. – tomasr Nov 22 '11 at 14:03
  • please see updated answer. this is the inbuilt value for checking the validation – Tim B James Nov 22 '11 at 14:15
  • Sorry @Tim I don't know how you mean it. Your example is not using plugin and second example doesn't work in my specific way. How can I use your example with plugin. – tomasr Nov 22 '11 at 14:17
  • It doesn't help. `!email.validationFailed` is always true – tomasr Nov 22 '11 at 15:48