2016-12-08 5 views
0

Ich habe ein Anmeldeformular, und ich verwende eine 3rd-Party-Bibliothek, um die Eingaben zu validieren. Diese Bibliothek bietet sehr schöne und coole Effekte für die Validierung, aber ich muss eine weitere Überprüfung durchführen, um sicherzustellen, dass das Passwort und das Passwort übereinstimmen. Wie kann ich das über jQuery machen? Hier ist mein Code:Kombinieren von zwei Validierungen in jQuery für das Passwortfeld

<div class="form-group"> 
    <label>Password</label> 
    <input type="password" class="form-control" name="password" /> 
</div> 
<div class="form-group"> 
    <label>Retype Password</label> 
    <input type="password" class="form-control" name="password2" /> 
</div> 

und jQuery Teil:

$('#registrationForm').formValidation({ 
    framework: 'bootstrap', 
    fields: { 
     password: { 
      validators: { 
       notEmpty: { 
        message: 'The password is required' 
       } 
      } 
     }, 
     password2: { 
      validators: { 
       notEmpty: { 
        message: 'Password conformation is required' 
       }, 
       equalTo: { 
        field: 'password', 
        message: 'The password cannot be the same as username' 
       } 
      } 
     } 
    } 
}); 

Was soll ich anstelle der Fragezeichen setzen, um mein Problem zu lösen? Oder vielleicht bin ich völlig falsch mit der Syntax?

+0

Wo ist der jQuery? Was Sie dort haben, ist ein verschachteltes Objektliteral. –

+0

Verwenden Sie Validate.js? –

+0

Nein Ich benutze formvalidation.io – Amir

Antwort

1

Working Fiddle

Html:

<form id="formCheckPassword"> 

    <div class="form-group"> 
    <label>Password</label> 
    <input type="password" class="form-control" name="password" id="password" /> 
    </div> 
    <div class="form-group"> 
    <label>Retype Password</label> 
    <input type="password" class="form-control" name="cfmPassword" id="cfmPassword" /> 
    </div> 
    <input type="submit" value="submit" /> 
</form> 

und JQuery Regeln:

$("#formCheckPassword").validate({ 
    rules: { 
    password: { 
     required: true, 
     minlength: 6, 
     maxlength: 10, 

    }, 

    cfmPassword: { 
     equalTo: "#password", 
     minlength: 6, 
     maxlength: 10 
    } 


    }, 
    messages: { 
    password: { 
     required: "the password is required" 

    } 
    } 

}); 

Update wie auf Anfrage:

$('#RegistrationForm').formValidation({ 
    framework: 'bootstrap', 
    fields: { 
     password: { 
      validators: { 
       identical: { 
        field: 'confirmPassword', 
        message: 'The password and its confirm are not the same' 
       } 
      } 
     }, 
     confirmPassword: { 
      validators: { 
       identical: { 
        field: 'password', 
        message: 'The password and its confirm are not the same' 
       } 
      } 
     } 
    } 
}); 
+0

Danke Shree. Können Sie Ihren Code basierend auf diesem Code-Snippet neu schreiben? $ ('# ANMELDE') formValidation ({ Rahmen:. 'Bootstrap', Felder: { pw: { Validatoren: { notEmpty: { Meldung: 'Das Passwort ist erforderlich' } } }, PW2: { validators: { notEmpty: { Meldung: 'Conformation erforderlich' } } } } }); – Amir

+0

Siehe Update zur Validierung. –

+0

Perfekt! Vielen Dank! – Amir

0

Ich sehe es nicht in dieser Syntax arbeiten. Wenn Sie es über jQuery tun wollen (wie Sie bereits erwähnt), können Sie einfach die Werte überprüfen:

var password1 = $('input[name=password]').val(); 
var password2 = $('input[name=password2]').val(); 
var match = password1 == password2