2017-04-20 41 views
-1

我有一個登記表,在我所使用的JavaScript表單驗證如何爲表單驗證添加模式RegEx?

我的形式

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> 
    <form class="form-group" method="post" id="contact-form"> 
     {% csrf_token %} 

     <input type"text" name="username" placeholder="userid" id="id_username"/> 
     <input type"email" name="email" placeholder="email" id="id_email"/> 
     <input type"password" name="password1" placeholder="password1" id="id_password1"/> 
     <input type"password" name="password2" placeholder="password2" id="id_password2"/> 
     <input type="address" name="address" placeholder="address" id="id_address"/> 

     <input type="submit"/> 

    </from> 

的JavaScript

<script type="text/javascript"> 

$(document).ready(function() { 

    $('#contact-form').validate({ 
     rules: { 
      username: { 
       minlength: 5, 
       required: true 
      }, 
      email: { 
       required: true, 
       email: true 
      }, 
      password1: { 
       minlength: 6, 
       required: true 
      }, 
      password2: { 
       required: true, 
       minlength: 6, 
       equalTo: "#password1", 
      }, 
      address: { 
       minlength: 5, 
       required: true, 

      }, 
     }, 
     highlight: function (element) { 
      $(element).closest('.control-group').removeClass('success').addClass('error'); 
     }, 
     success: function (element) { 
      element.text('OK!').addClass('valid') 
       .closest('.control-group').removeClass('error').addClass('success'); 
     } 
    }); 

    }); 

我想正則表達式來解決像只/- ()。,這些章程只允許用英文字母 如何在此使用模式RegEx?

+0

嘗試'/^[A-ZA-Z0-9 \/\ - ()] + $ /' – Tushar

+0

ü可以給我的代碼 – stone

回答

0

試試這個

address: { 
      minlength: 5, 
      maxlenght=50, 
      required: true, 
      regex: /^[a-zA-Z0-9\s,'-./()]*$/, 
     }, 
+0

這是一個錯誤的解決辦法。 –