jquery 验证规范器 - 应用了 trim 的值未发送到服务器
jquery validation normalizer - the value which has trim applied is not being sent to server
将 Normalizer method in jQuery Validation 应用于 trim 用户名和密码字段中的空格。但是,发送回服务器的结果没有应用 trim。我知道我永远不应该假设,但出于好奇,这是预期的结果吗?
Eg: Use the values inside the quotes Username:" test " Password:" C
F1234 "
After normalization from console.log values are
Username:"test" Password:"C F1234"
But with submitHandler
the form.submit()
sends the values as
inputted by the user.
我是不是执行错了?
$(document).ready(function(){
$("#login-form").validate({
rules: {
username:{
required:true,
normalizer:function(value){
console.log('user pre-trim:'+value);
console.log('user post-trim:'+$.trim(value));
return $.trim(value);
},
email:true,
},
password:{
required:true,
normalizer:function(value){
console.log('pwd pre-trim:'+value);
console.log('pwd post-trim:'+$.trim(value));
return $.trim(value);
},
},
},
errorContainer: "#errorMessageContainer",
errorLabelContainer: "#errorMessageContainer ul",
errorElement: "li",
messages: {
username:{
required:"Email cannot be left empty",
email:"Must be an Email address"
},
password: {
required: "Password Cannot Be Left Empty",
}
}
,
invalidHandler: function(form, validator) {
//Can something be done here?
console.log('invalidContainer');
},
submitHandler:
function(form) {
form.submit();
}
});
displayErrorContainer();
});
function displayErrorContainer(){
var child = $("#errorMessageContainer").children("ul").html();
console.log('ftn: msg displayErrorContainer:'+child);
if(typeof child !== "undefined" && child.length > 0){
//console.log("show the error-container");
$("#errorMessageContainer").show();
}else{
//console.log("hide the error-container");
$("#errorMessageContainer").hide();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<div id="errorMessageContainer">
<ul><li>Username and Password do not match</li></ul>
</div>
<form id="login-form" request="POST">
<input id="username" name="username" type="text" />
<input id="password" name="password" type="password" />
<button type="submit">Submit</button>
</form>
Have I implemented this incorrectly?
Reading the docs: "不改变元素的值,它只改变用于验证的值。"
所以答案是'no'。
将 Normalizer method in jQuery Validation 应用于 trim 用户名和密码字段中的空格。但是,发送回服务器的结果没有应用 trim。我知道我永远不应该假设,但出于好奇,这是预期的结果吗?
Eg: Use the values inside the quotes Username:" test " Password:" C F1234 "
After normalization from console.log values are
Username:"test" Password:"C F1234"
But with
submitHandler
theform.submit()
sends the values as inputted by the user.
我是不是执行错了?
$(document).ready(function(){
$("#login-form").validate({
rules: {
username:{
required:true,
normalizer:function(value){
console.log('user pre-trim:'+value);
console.log('user post-trim:'+$.trim(value));
return $.trim(value);
},
email:true,
},
password:{
required:true,
normalizer:function(value){
console.log('pwd pre-trim:'+value);
console.log('pwd post-trim:'+$.trim(value));
return $.trim(value);
},
},
},
errorContainer: "#errorMessageContainer",
errorLabelContainer: "#errorMessageContainer ul",
errorElement: "li",
messages: {
username:{
required:"Email cannot be left empty",
email:"Must be an Email address"
},
password: {
required: "Password Cannot Be Left Empty",
}
}
,
invalidHandler: function(form, validator) {
//Can something be done here?
console.log('invalidContainer');
},
submitHandler:
function(form) {
form.submit();
}
});
displayErrorContainer();
});
function displayErrorContainer(){
var child = $("#errorMessageContainer").children("ul").html();
console.log('ftn: msg displayErrorContainer:'+child);
if(typeof child !== "undefined" && child.length > 0){
//console.log("show the error-container");
$("#errorMessageContainer").show();
}else{
//console.log("hide the error-container");
$("#errorMessageContainer").hide();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<div id="errorMessageContainer">
<ul><li>Username and Password do not match</li></ul>
</div>
<form id="login-form" request="POST">
<input id="username" name="username" type="text" />
<input id="password" name="password" type="password" />
<button type="submit">Submit</button>
</form>
Have I implemented this incorrectly?
Reading the docs: "不改变元素的值,它只改变用于验证的值。"
所以答案是'no'。