使用javascript验证wordpress中的自定义字段

时间:2014-12-22 07:31:08

标签: jquery html wordpress validation

我已经在wordpress中添加了jquery lib,用于在自定义表单中添加javascript验证。只有第一个验证字段正在工作,其他验证错误未显示。我在function.php中添加了jquery

add_action( 'wp_enqueue_scripts', 'my_nifty_scripts' );
   function my_nifty_scripts() {
   wp_register_script('my-nifty-custom', get_stylesheet_directory_uri() . '/js/jquery.min.js',
   false, null, true);
wp_enqueue_script('my-nifty-custom'); 
}

我的主页的Html代码: -

  <form  method="POST"  name="adminprofForm" onsubmit="return(validateAdminprofile());">

            <div class="claim-form">
                <div class="claim-form-label">Your Name </div>
                 <input type="text" name="yourename" id="yourename" />
                 <div id="unameError"></div>    
                <?php if(!empty($nameError)){ print_r($nameError);}?>
            </div>

            <div class="claim-form">
                <div class="claim-form-label">Your contact telephone number</div>
                 <input type="text" name="phoneno" />
                  <?php if(!empty($phoneError)){ print_r($phoneError);}?>
            </div>

            <div class="claim-form">
                <div class="claim-form-label">Your email address</div>
               <input type="text" name="email" />
                <?php if(!empty($emailError)){ print_r($emailError);}?>
            </div>
            <div class="claim-form">
                <div class="claim-form-label">Accident or illness</div>
                 <input type="text" name="type" />
                <?php if(!empty($typeError)){ print_r($typeError);}?>
            </div>
            <div class="claim-form">
                <div class="claim-form-label">Best time to call</div>
               <input type="text" name="time" />
                <?php if(!empty($timeError)){ print_r($timeError);}?>
           </div>
            <div class="claim-btn">
            <input type="submit" value="Submit" name="submit" class="claim-form-submit-btn" />
            </div>
            </form>

 <script type="text/javascript">
 function validateAdminprofile()
{   

    var chk = 0;
    var patternProfname = /^[A-Z a-z]{3,35}$/i;
    var textProfname = document.getElementById("yourename");        
    if($("#yourename").val()==""){
    alert("Enter your name");
    //$("#unameError").css("color","red").html("Enter your name");
    //$("#gotop").click();
    chk = chk +1;
    }
    else if(!patternProfname.test(textProfname.value)){
    alert("Enter your valid name");
    //$("#unameError").css("color","red").html("Enter valid name");
    //$("#gotop").click();
    chk = chk +1;
    }
    var patternProfemail =/^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i;
    var textProfemail = document.getElementById("youreemail");              
    if($("#youreemail").val()==""){
    alert("Enter your name");   
    //$("#uemailError").css("color","red").html("Enter admin email id");
    //$("#gotop").click();
    chk = chk +1;
    }
    else if(!patternProfemail.test(textProfemail.value)){
    alert("Enter your valid email");    
    //$("#uemailError").css("color","red").html("Enter valid email id");
    //$("#gotop").click();
    chk = chk +1;
    }   

    //else $("#unameError").html("");

    if(chk==0)
        return true;
    else 
        return false;
  }
  </script>

0 个答案:

没有答案