HTML5驗證
回答
我很確定這是不可能的。此外,它可以很容易地被javascript覆蓋,所以爲什麼不使用它呢?
這工作得很好:
<script language="javascript">
function checkPassword() {
if (document.pwForm.pw1.value != document.pwForm.pw2.value) {
alert ('The passwords do not match!');
return false;
}
}
</script>
<form action="filename.ext" name="pwForm" method="GET/POST">
<input type="password" name="pw1" value=""><br />
<input type="password" name="pw2" value=""><br />
<input type="Submit" name="CheckPassword" value="Check Passwords" onClick="return checkPassword();">
</form>
雖然答案是正確的。此代碼有很多問題:1.如果field1未更改,則不添加自定義錯誤。 2.如果有人在pw1而不是pw2中輸入了密碼,並且在該字段保持無效(雖然有效)後更改此密碼)。 3.只應該改變自定義有效性,如果該字段還沒有自定義錯誤或自定義錯誤是由同一個腳本添加的(您需要爲此測試validity.customError/validationMessage) – 2012-11-22 23:55:55
如果你想要的東西更好一點和HTML5的使用,試試這個: http://www.html5rocks.com/en/tutorials/forms/html5forms/
<label>Password:</label>
<input type="password" id="password" name="password">
<label>Confirm Password:</label>
<input type="password" id="passwordconf" name="passwordconf" oninput="check(this)">
<script language='javascript' type='text/javascript'>
function check(input) {
if (input.value != document.getElementById('password').value) {
input.setCustomValidity('The two passwords must match.');
} else {
// input is valid -- reset the error message
input.setCustomValidity('');
}
}
</script>
讓它看中加給你的CSS(下面)。當它們通過HTML5驗證失敗時,它會在有問題的輸入字段周圍放置紅色邊框。
:invalid {
border: 2px solid #ff0000;
}
全部完成。您仍然應該使用alert()或服務器端驗證來確保用戶正確輸入兩個密碼。不要依賴客戶端任何東西。
我有一個類似的問題,並解決它使用HTML5 api我這樣做:設置一個模式的密碼,以包含至少八個字母和一個數字。然後讓他們匹配我所做的:
var password = document.querySelector('#password'),
passwordConfirm = document.querySelector('#password_confirm');
[].forEach.call([password, passwordConfirm], function(el) {
el.addEventListener('input', function() {
if (!el.validity.patternMismatch) {
if (password.value === passwordConfirm.value) {
try{password.setCustomValidity('')}catch(e){}
} else {
password.setCustomValidity("Password and password confirm doesn\'t match")
}
}
}, false)
});
其中具有el.validity.patternMismatch
檢查模式的有效性,然後再檢查兩者的有效性。 這是我的密碼輸入模式。
感謝踢球,這是非常有用的。
我擴大了一點,使密碼和密碼確認輸入無效,只要輸入開始輸入。
var password = document.querySelector(' input[name=usr_password]');
var passwordConfirm = document.querySelector(' input[name=usr_password_confirm]');
if (password && passwordConfirm)
{
[].forEach.call([password, passwordConfirm], function(el) {
el.addEventListener('input', function() {
if (el.validity.patternMismatch === false) {
if (password.value === passwordConfirm.value) {
try{
password.setCustomValidity('');
passwordConfirm.setCustomValidity('');
}
catch(e){}
}
else {
password.setCustomValidity("The two passwords do not match");
}
}
if ((password.checkValidity() && passwordConfirm.checkValidity()) === false)
{
password.setCustomValidity("The two passwords do not match, and they don't comply with the password rules.");
passwordConfirm.setCustomValidity("The two passwords do not match, and they don't comply with the password rules.");
}
else
{
password.setCustomValidity('');
passwordConfirm.setCustomValidity('');
}
}, false)
});
}
我想這就是你要找的。
<p>Password: <input type="password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" name="pwd1" onchange="
this.setCustomValidity(this.validity.patternMismatch ? 'Password must contain at least 6 characters, including UPPER/lowercase and numbers' : '');
if(this.checkValidity()) form.pwd2.pattern = this.value;
"></p>
<p>Confirm Password: <input type="password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" name="pwd2" onchange="
this.setCustomValidity(this.validity.patternMismatch ? 'Please enter the same Password as above' : '');
"></p>
這將執行密碼和重新輸入密碼字段驗證。
另一種選擇是使用http://jqueryvalidation.org/validate/,如果你不介意使用Jquery來做你的骯髒工作。
退房http://jqueryvalidation.org/equalTo-method
<form id="myform">
<label for="password">Password</label>
<input id="password" name="password" />
<br/>
<label for="password_again">Again</label>
<input class="left" id="password_again" name="password_again" />
<br>
<input type="submit" value="Validate!">
</form>
<script>
$("#myform").validate({
rules: {
password: "required",
password_again: {
equalTo: "#password"
}
}
});
</script>
如果需要,您也可以編寫更復雜的方法:http://jqueryvalidation.org/category/plugin/
- 1. HTML5驗證與PHP驗證
- 2. Struts2驗證或HTML5驗證?
- 3. 在驗證HTML5
- 4. HTML5表單驗證
- 5. HTML5語法驗證
- 6. HTML5驗證消息?
- 7. html5驗證和JS
- 8. HTML5需要驗證
- 9. Angular JS HTML5驗證
- 10. HTML5驗證問題
- 11. 語音驗證HTML5
- 12. HTML5 FORM,AJAX驗證
- 13. HTML5驗證模式?
- 14. HTML5表單驗證
- 15. HTML5輸入驗證
- 16. HTML5 Boilerplate,HTML5重置CSS驗證
- 17. Emacs,HTML5無法驗證
- 18. Zend Studio的HTML5驗證
- 19. HTML5錯誤W3C驗證
- 20. yii ajaxSubmitButton塊html5驗證
- 21. rel =「canonical」html5無法驗證
- 22. Nu HTML5驗證程序
- 23. 驗證HTML5中的C#
- 24. HTML5視頻驗證錯誤
- 25. HTML5表單驗證模式
- 26. 驗證HTML5輸入範圍
- 27. HTML5視頻無法驗證
- 28. jQuery事件和HTML5驗證
- 29. HTML5表格模式/驗證
- 30. 僅驗證HTML5字段集
雖然你可以做到這一點,你應該避免這種情況。通過Ajax在服務器端進行驗證。 – Yang 2013-04-18 03:47:56