表单验证规则动态更改

时间:2019-03-21 06:12:33

标签: javascript jquery ajax validation

var mandatory = {
  emailAddressTextBox: {
    required: true,
    email: true
  },
  passwordTextBox: {
    required: true,
    minlength: 6,
    maxlength: 24,
  }
};

var nonmandatory = {
  nameTextBox: {
    required: true,
    maxlength: 50
  },
  loginEmailAddressTextBox: {
    required: true,
    email: true
  },
  loginPasswordTextBox: {
    required: true,
    minlength: 6,
    maxlength: 24,
  },
  loginPasswordAgainTextBox: {
    required: true,
    minlength: 6,
    maxlength: 24,
    equalTo: "#loginPasswordTextBox"
  }
};

$("#myForm").validate({
  rules: ($("input[name='country']").val() === "US") ? mandatory : nonmandatory;
});

我想根据国家/地区名称动态更改Ajax中的表单验证规则。

让我们说说它是否是英国

  

仅电子邮件和密码应为必填项。

或者如果是美国

  

连同电子邮件一样,姓和名必须是必填项。

我是Ajax的新手。

有人可以帮助我吗?

预先感谢!

1 个答案:

答案 0 :(得分:1)

在验证规则中尝试此答案...,而不是required:true,您必须使用此功能

required: function(element) {
                if ($("#country option:selected").val()== '1') {
                    return false;
                }   
                else {
                    return true;
                }
         }

$(document).ready(function () {

   
    $("#form").validate({
        rules: {
            "name": {
                required: function(element) {
    			if ($("#country option:selected").val()== '1') {
    				return false;
    			}	
    			else {
    				return true;
   			 	}
         },
                minlength: 5
            },
            "email": {
                required: function(element) {
    			if ($("#country option:selected").val()== '1') {
    				return true;
    			}	
    			else {
    				return true;
   			 	}
         },
                email: true
            },
            "password": {
                required: function(element) {
    			if ($("#country option:selected").val()== '1') {
    				return true;
    			}	
    			else {
    				return true;
   			 	}
         },
                email: true
            }
        },
        messages: {
            "name": {
                required: "Please, enter a name"
            },
            "email": {
                required: "Please, enter an email",
                email: "Email is invalid"
            },
            "password": {
                required: "Please, enter password"
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form submitted'); // for demo
            return false; // for demo
        }
    });

});
body {
    padding: 20px;
}

label {
    display: block;
}

input.error {
    border: 1px solid red;
}

label.error {
    font-weight: normal;
    color: red;
}

button {
    display: block;
    margin-top: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script>
<form id="form" method="post" action="#">
    <select id="country">
    <option value="1">US</option>
    <option value="2">UK</option>
    </select>
    <label for="name">Name</label>
    <input type="text" name="name" id="name" />
    <label for="email">Email</label>
    <input type="email" name="email" id="email" />
     <label for="name">Password</label>
    <input type="text" name="password" id="password" />
    <button type="submit">Submit</button>
</form>