问题
It appears that even with the current validate plugin you can't have a comma in a value if you want to validate using min. I found on github where the patch was submitted several months ago (11 months) to modify the source .js file but it's still not in release.
So instead of modifying my source .js file I'm trying to figure out how to replace the comma prior to validation.
I tried something like this:
$("#amount").blur(function(){
var val = $(this).val().replace(/\,/g,'');
$(this).val( val );
});
$("#transferForm").validate({
.......
});
But it runs validate first, fails because of min and then replaces the value. I don't necessarily want it to fail validation and tell the user to remove commas - I just want it to remove the comma before running the min rule.
回答1:
Don't even let them enter comma's
$("#amount").keyup(function(){
var val = $(this).val().replace(/\,/g,'');
$(this).val( val );
});
Demo: http://jsfiddle.net/calder12/tdxKv/
回答2:
Try this http://jsfiddle.net/MWjZr/
$(function(){
$("input[id=sometexboxid]").each(function(){
this.value=this.value.replace(/,/g, "");
}).on('keyup', function(){
this.value=this.value.replace(/,/g, "");
})
});
For more explanation please refer http://davidwalsh.name/javascript-replace article.
来源:https://stackoverflow.com/questions/15415814/jquery-validate-replace-commas-in-field-before-validation-occurs