我有如下的登記表格:如何使用jQuery創建錯誤消息驗證表單輸入?
<div class="form">
<form class="register-form" name="register-form">
<ul class="errorMessages"></ul>
<div class="user-name">
<input class="wrap" name="firstname" type="text" placeholder="firstname" required/>
<input class="wrap" name="lastname" type="text" placeholder="lastname" required/>
</div>
<input name="password" type="password" placeholder="password" autocomplete="off" required/>
<input name="email" type="text" placeholder="email address" autocomplete="off" required/>
button class="btn-signup" type="submit">Create</button>
<p class="message">Already registered? <a href="#">Sign In</a></p>
</div>
我創建的jQuery代碼檢查,如果輸入的是空白,這將創建錯誤消息」
$(function() {
var createAllErrors = function() {
var form = $(".register-form");
var errorList = $('ul.errorMessages', form);
var showAllErrorMessages = function() {
errorList.empty();
form.find(':invalid').each(function (index, node) {
var label = $('input').attr('name');
var message = node.validationMessage || 'Invalid value.';
errorList
.show()
.append('<li><span>' + label + ': ' + '</span>' + message + '</li>');
});
};
$('input[type=submit], button', form).on('click', showAllErrorMessages);
};
$('form').each(createAllErrors);
});
但是,當我按下按鈕,我有結果:
約標籤變量的任何問題嗎?或者形式找到無效的輸入?
檢查以下解決您的問題。祝你好運。它的工作原理是 – ScanQR