0

Ive just recently started using jquery validation engine, and I was wondering:

Im in a scenario where I only want to validate a field only if another field isnt empty. I have applied the condRequired validation rule to this field, which works fine, but all other rules after the condRequired get fired. For example:

<input type="password" id="current-password" name="current-password" placeholder="Current Password" class="validate[condRequired[password],ajax[ajaxValidateUserPassword]]"> 
<input type="password" id="password" name="password" placeholder="New Password" class="ajax[ajaxValidateUserRegistration]]">

If a user decides they want to update their password, they must enter the current password. However I only want to validate the current password, if the user has entered a new password. So I have:

validate[condRequired[password],ajax[ajaxValidateUserPassword]] 

as the validation rule on the current password. The problem is: even if the user hasn't entered a new password,

condRequired[password] 

will fail, but

ajax[ajaxValidateUserPassword]

still gets fired.

Is there a way to get the

ajax[ajaxValidateUserPassword]

validation rule to get skipped if the

condRequired[password] 

rule fails? Idea is I dont want to bother with ANY validation on the current password if no new password has been entered

Thanks!

chridam
  • 100,957
  • 23
  • 236
  • 235
JohnE
  • 155
  • 1
  • 1
  • 7
  • you're looking for `requiredif`: http://stackoverflow.com/questions/11052571/unobtrusive-validation-c-sharp-mvc-razor (Just the javascript part) – rkw Dec 04 '12 at 09:12
  • I have edited your title. Please do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. Also see, ["Should questions include “tags” in their titles?"](http://meta.stackoverflow.com/q/19190/193440), where the consensus is "no, they should not. – chridam Jun 17 '14 at 08:58

2 Answers2

0

ValidationEngine allows you to specify a validation rule which uses a custom function to determine validity.

<input value="" class="validate[required,funcCall[checkHELLO]]" type="text" id="lastname" name="lastname" />

function checkHELLO(field, rules, i, options){
  if (field.val() != "HELLO") {
     // this allows the use of i18 for the error msgs
     return options.allrules.validate2fields.alertText;
  }
}

From within that function you can call any ajax to validate field if "New Password" is not empty

Andriy F.
  • 2,467
  • 1
  • 25
  • 23
  • Thanks Andriy, I saw this but I was hoping there was a more elegant way? Im guessing this is my best bet – JohnE Dec 04 '12 at 20:14
0

Use the option "maxErrorsPerField":

$("form").validationEngine({
    maxErrorsPerField: 1
});
Niev
  • 1
  • 1