如何为表单验证添加模式RegEx?

时间:2017-04-20 07:26:00

标签: javascript jquery regex jquery-form-validator

我有一个注册表单,其中我使用了JavaScript表单验证器

我的表单

<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模式只能解决 / - ()。,这些章程只允许使用字母表 我如何在此使用模式RegEx?

1 个答案:

答案 0 :(得分:0)

试试这个

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