2012-12-04 158 views
0

,我最近剛使用jquery validation engine開始了,我想知道:如何停止進一步的驗證,如果一個失敗

林在這樣一個場景,我只是想驗證字段只有當另一場心不是空。我已經將condRequired驗證規則應用於此字段,該規則工作正常,但condRequired之後的所有其他規則都會被解僱。例如:

<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]]"> 

如果用戶決定要更新密碼,他們必須輸入當前密碼。但是我只想驗證當前的密碼,如果用戶輸入了新的密碼。所以,我有:

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

作爲當前密碼的驗證規則。問題是:即使用戶沒有輸入新的密碼,

condRequired[password] 

會失敗,但

ajax[ajaxValidateUserPassword] 

還是被炒魷魚。

有沒有辦法讓

ajax[ajaxValidateUserPassword] 

驗證規則,如果

condRequired[password] 

規則未能得到跳過?想法是我不想打擾任何驗證當前密碼,如果沒有新的密碼已輸入

謝謝!

+0

requiredif':http://stackoverflow.com/questions/11052571/unobtrusive-validation-c-sharp-mvc-razor(只是javascript部分) – rkw

+0

我編輯了你的標題。請不要包含有關問題標題中使用的語言的信息,除非在沒有它的情況下沒有意義。標籤用於此目的。另請參閱[「應該問題是否在其標題中包含」標籤「?」](http://meta.stackoverflow.com/q/19190/193440),其中共識爲「否,他們不應該」 – chridam

回答

0

ValidationEngine允許您指定使用自定義函數來確定有效性的驗證規則。

<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; 
    } 
} 

從這個函數中,你可以調用任何AJAX驗證領域,如果「新密碼」不爲空

+0

謝謝Andriy,我看到了這一點,但我希望有一個更優雅的方式?我猜這是我最好的選擇 – JohnE

0

使用選項「maxErrorsPerField」:你要找的`

$("form").validationEngine({ 
    maxErrorsPerField: 1 
}); 
相關問題