Validate upon submit
$("#sbmit").click(function(){
$(":text[name^='1stfld']").each(function(i, laman){
var $current = $(this);
$('input[name^="1stfld"]').each(function() {
if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id')) {
开发者_运维技巧 alert('Duplicate Found!');
$(this).addClass("xrequired");
return false;
}else{
$(laman).removeClass("xrequired");
}
});
if ($(this).val() == "") {
alert("wala");
$(laman).addClass("xrequired");
$(laman).focus();
return false;
}else{
$(laman).removeClass("xrequired");
}
$("#form").submit();
});
How can I optimized this?
Perhaps you want to have a look at the excellent jQuery validation plugin: http://bassistance.de/jquery-plugins/jquery-plugin-validation/
The creator of this plugin is a ninja in jQuery, so you can expect that his code is excellent (Microsoft uses his plugin in their ASP.NET MVC platform).
精彩评论