使用jQuery(form)验证插件的简单数学验证JavaScript脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用jQuery(form)验证插件的简单数学验证JavaScript脚本相关的知识,希望对你有一定的参考价值。
Don't forget to include jQuery validation plugin before this snippet. You can find it here:http://bassistance.de/jquery-plugins/jquery-plugin-validation/
jQuery(document).ready(function() { var send_email_form = jQuery("form#my-form"); var lowerBound = 1; var upperBound = 10; // to generate random numbers between a range that starts somewhere other than // zero use this formula where m is the lowest possible integer value of the range // and n equals the top number of the range. // Math.floor(Math.random() * (n - m + 1)) + m // generate random numbers between lowerBound and upperBound (inclusive) var a = Math.floor(Math.random() * (upperBound - lowerBound + 1)) + lowerBound; var b = Math.floor(Math.random() * (upperBound - lowerBound + 1)) + lowerBound; jQuery("input#eqtn_soln").val(a+b); var validation_rules = { name: "required", email: { required: true, email: true }, subject: "required", message: "required", sum: { required: true, equalTo: "input#eqtn_soln" } }; var validation_messages = { name: "please enter your name", email: { required: "Please enter your email address", email: "Please enter a valid email address" }, subject: "please enter a message subject", message: "please enter the message", sum: { required: "please answer the equation", equalTo: "please answer the equation correctly" } }; //form validation with javascript send_email_form.validate({ rules: validation_rules, messages: validation_messages, //errorClass: "form-error-style", errorPlacement: function(error, element){ // places the error message after the element one line break below error.insertAfter(jQuery("<br />").insertAfter(element)); } }); });
以上是关于使用jQuery(form)验证插件的简单数学验证JavaScript脚本的主要内容,如果未能解决你的问题,请参考以下文章
jQuery 验证插件未在 form$.validate() 上进行验证