【发布时间】:2016-10-10 23:39:18
【问题描述】:
我想定义一个函数来验证输入字段是否是有效的个人 ID!我有你所看到的功能:
function checkMeliCode(code)
{
if(!/^\d{8,10}$/.test(code) || /^(0{8,10}|1{8,10}|2{8,10}|3{8,10}|4{8,10}|5{8,10}|6{8,10}|7{8,10}|8{8,10}|9{8,10})$/.test(code))
return false;
var L=code.length, _=0;
for(i=0;i<L-1;i++)
_+=code.charAt(i)*(L-i);
_%=11;
return (code.charAt(L-1)==((_<2)?_:11-_))
}
我正在使用 On Rails 上的向导表单。这是我在查看的 JavaScript 代码:
$(function() {
$("#wizard").steps();
$("#form").steps({
bodyTag: "fieldset",
onStepChanging: function (event, currentIndex, newIndex) {
// Always allow going backward even if the current step contains invalid fields!
if (currentIndex > newIndex) {
return true;
}
// Forbid suppressing "Warning" step if the user is to young
if (newIndex === 3 && Number($("#age").val()) < 18) {
return false;
}
var form = $(this);
// Clean up if user went backward before
if (currentIndex < newIndex) {
// To remove error styles
$(".body:eq(" + newIndex + ") label.error", form).remove();
$(".body:eq(" + newIndex + ") .error", form).removeClass("error");
}
// Disable validation on fields that are disabled or hidden.
form.validate().settings.ignore = ":disabled,:hidden";
// Start validation; Prevent going forward if false
return form.valid();
},
onStepChanged: function (event, currentIndex, priorIndex) {
// Suppress (skip) "Warning" step if the user is old enough.
if (currentIndex === 2 && Number($("#age").val()) >= 18) {
$(this).steps("next");
}
// Suppress (skip) "Warning" step if the user is old enough and wants to the previous step.
if (currentIndex === 2 && priorIndex === 3) {
$(this).steps("previous");
}
switch (priorIndex){
case 0:
console.log("my log");
send_basic_info();
break;
// case 1:
// send_education();
// break;
// case 2:
// send_experience();
// break;
// case 3:
// send_laguages();
// break;
// case 4:
// send_computer_skill();
// break;
// case 5:
// send_computer_certificate();
// break;
}
},
onFinishing: function (event, currentIndex) {
var form = $(this);
// Disable validation on fields that are disabled.
// At this point it's recommended to do an overall check (mean ignoring only disabled fields)
form.validate().settings.ignore = ":disabled";
// Start validation; Prevent form submission if false
return form.valid();
},
onFinished: function (event, currentIndex) {
var form = $(this);
// Submit form input
// form.submit();
$.ajax({
url: "/resume/finished",
data: null,
dataType: "json",
type: "post",
success: function(data){
document.location.href="/";
}
});
}
}).validate({
errorPlacement: function (error, element) {
element.before(error);
},
rules: {
confirm: {
equalTo: "#password"
}
}
});
});
这是我希望验证的输入:
<div>
<label>*National ID</label>
<input name="bi-national-id" type="text" placeholder="Enter your National ID" id="national_id" class="form-control required" ><br>
</div>
问题是我真的不知道 .validate() 和 .validation() 之间的区别。资产已添加(表单验证和验证)。
【问题讨论】:
标签: javascript jquery ruby-on-rails validation