我正在从Polymer 0.5迁移到Polymer 1.0,而且我无法进行简单的验证 - 密码和重新输入密码字段的经典场景。
聚合物0.5我有:
<paper-input-decorator id="passwordDecId" label="New password (8 caratteri almeno)" floatingLabel error="campo obbligatorio!">
<input id="passwordId" onchange="validatePassword()" is="core-input" type="password" name="password" required pattern=".{8,32}">
</paper-input-decorator>
<paper-input-decorator id="reenterPasswordDecId" label="Re-enter password (8 caratteri almeno)" floatingLabel error="campo obbligatorio!">
<input id="reenterPasswordId" onchange="validatePassword()" is="core-input" type="password" name="reenterPassword" required pattern=".{8,32}">
</paper-input-decorator>
并且validatePassword函数是:
function validatePassword() {
var passwordDecorator = document.getElementById('passwordDecId');
var passwordInput = document.getElementById('passwordId');
var reenterPasswordDecorator = document.getElementById('reenterPasswordDecId');
var reenterPasswordInput = document.getElementById('reenterPasswordId');
var pass2 = reenterPasswordInput.value;
var pass1 = passwordInput.value;
if (pass1 != pass2) {
reenterPasswordInput.setCustomValidity("Passwords Don't Match");
} else {
// empty string means no validation error
reenterPasswordInput.setCustomValidity('');
}
passwordDecorator.isInvalid = !passwordInput.validity.valid;
reenterPasswordDecorator.isInvalid = !reenterPasswordInput.validity.valid;
}
并且它有效,但是现在,在迁移到Polymer 1.0之后,我不再使用纸张输入装饰器,所以我想只用纸张输入做所有事情:
<paper-input id="passwordId" onchange="validatePassword()" name="password" type="text" label="New password" auto-validate required pattern=".{1,32}" error-message="1 caratteri almeno"></paper-input>
<paper-input id="reenterPasswordId" onchange="validatePassword()" name="reenterPassword" type="text" label="Re-enter password" auto-validate required pattern=".{1,32}" error-message="1 caratteri almeno"></paper-input>
当两个密码的长度大于1个字符但值不同时,我不知道如何更改validatePassword
函数以显示错误用户按下提交按钮或输入焦点丢失时。
答案 0 :(得分:3)
这里有一些关于如何使用聚合物1.0验证匹配密码的示例
<paper-input label="Password" required value="{{pass::input}}" id="checkPassword"></paper-input>
<paper-input label="Re-type Password" required value="{{repass::input}}" on-change="passmatch" id="checkPassword"></paper-input>
并添加 passmatch 功能
passmatch: function(e){
var password = encodeURIComponent(this.pass);
var confirmPassword = encodeURIComponent(this.repass);
if(password != confirmPassword){
// do something
}
}