0

在提交表单之前,我正在使用脚本来验证一些表单字段。如果验证失败,脚本应该返回 false。在 linux 上,该脚本在 FF、Chrome 和 Opera 中运行良好。在 Windows 上,脚本失败,表单在 Chrome、Safari 和 IE 上提交。您的想法将不胜感激。

$(document).ready(function(){    
    // Place ID's of all required fields in the array.
    required=["formname","formemail"];
    email = $("#formemail");
    name = $("#formname");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please fill out this field.";
    emailerror = "Please enter a valid e-mail.";

    $("#suggestionform").submit( function(){
         //validate required fields
         for(i =  0; i < required.length; i++){
             var input = $('#'+required[i]);
             if( (input.val() == "") || (input.val() == emptyerror)){
                 input.addClass("needsfilled");
                 input.val(emptyerror);
                 errornotice.fadeIn(750);
             }else {
                 input.removeClass("needsfilled");
             }
         }         
         if( !/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())){
             email.addClass("needsfilled");
             email.val(emailerror);
         }         
         if ( "" == name.val()){
             name.addClass("needsfilled");
             name.val(emptyerror);
         }
         if( $("#link1").val() + $("#link2").val() + $("#link3").val() + $("#link4").val() +  $("#textarea1").val() + $("#textarea2").val() == "") {
             errornotice.fadeIn(750);
             alert("returning fail")
             return false;
         }
         if( $(":input").hasClass("needsfilled")){
             alert("returning fail here")
             return false;
         }else{errornotice.hide();return true;}      
    });   

    $(":input").focus( function(){
        if ($(this).hasClass("needsfilled")){
            $(this).val("");
            $(this).removeClass("needsfilled");
        }
    })    
});





Version 2
 var required=["formname","formemail"];
var email = $("#formemail");
var name = $("#formname");
var errornotice = $("#error");
// The text to show up within a field when it is incorrect
var emptyerror = "Please fill out this field.";
var emailerror = "Please enter a valid e-mail.";

function suggestionSubmit(theform){
    required=["formname","formemail"];
    email = $("#formemail");
    name = $("#formname");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please fill out this field.";
    emailerror = "Please enter a valid e-mail.";    

    for(i =  0; i < required.length; i++){
             var input = $('#'+required[i]);
             if( (input.val() == "") || (input.val() == emptyerror)){
                 input.addClass("needsfilled");
                 input.val(emptyerror);
                 errornotice.fadeIn(750);
             }else {
                 input.removeClass("needsfilled");
             }
         }         
     if( !/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())){
         email.addClass("needsfilled");
         email.val(emailerror);
     }         
     if ( "" == name.val()){
         name.addClass("needsfilled");
         name.val(emptyerror);
     }
     if( $("#link1").val() + $("#link2").val() + $("#link3").val() + $("#link4").val() +  $("#textarea1").val() + $("#textarea2").val() == "") {
         errornotice.fadeIn(750);
         return false
     }
     if( $(":input").hasClass("needsfilled")){
         return false;
     }
     else{errornotice.hide();return true;}     
}
4

1 回答 1

0

你可以试着这样写

$(document).ready(function(){    
// Place ID's of all required fields in the array.
required=["formname","formemail"];
email = $("#formemail");
name = $("#formname");
errornotice = $("#error");
// The text to show up within a field when it is incorrect
emptyerror = "Please fill out this field.";
emailerror = "Please enter a valid e-mail.";

$("#suggestionform").submit( function(event){
     //validate required fields
     for(i =  0; i < required.length; i++){
         var input = $('#'+required[i]);
         if( (input.val() == "") || (input.val() == emptyerror)){
             input.addClass("needsfilled");
             input.val(emptyerror);
             errornotice.fadeIn(750);
         }else {
             input.removeClass("needsfilled");
         }
     }         
     if( !/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())){
         email.addClass("needsfilled");
         email.val(emailerror);
     }         
     if ( "" == name.val()){
         name.addClass("needsfilled");
         name.val(emptyerror);
     }
     if( $("#link1").val() + $("#link2").val() + $("#link3").val() + $("#link4").val() +  $("#textarea1").val() + $("#textarea2").val() == "") {
         errornotice.fadeIn(750);
         alert("returning fail")
         event.preventDefault();
         return false;
     }
     if( $(":input").hasClass("needsfilled")){
         alert("returning fail here")
         event.preventDefault();
         return false;
     }else{errornotice.hide();return true;}      
});   

希望能帮助到你。

于 2012-05-12T14:28:01.527 回答