ENDRPrint_12CRelease/web/js/validateForm.js
2024-08-14 10:58:03 +07:00

165 lines
5.1 KiB
JavaScript

var errors = null;
var errorsCount = 0;
$(document).ready(function(){
jQuery.validator.addMethod("password", function( value, element ) {
var result = this.optional(element) || value.length >= 6 && /\d/.test(value) && /[a-z]/i.test(value);
if (!result) {
element.value = "";
var validator = this;
setTimeout(function() {
validator.blockFocusCleanup = true;
element.focus();
validator.blockFocusCleanup = false;
}, 1);
}
return result;
}, "Your password must be at least 6 characters long and contain at least one number and one character.");
jQuery.validator.addMethod("datepicker", function( value, element ) {
// if (this.optional(element))
// return true;
var check = false;
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
if( re.test(value)){
var adata = value.split('/');
var dd = parseInt(adata[0],10);
var mm = parseInt(adata[1],10);
var yyyy = parseInt(adata[2],10);
var xdata = new Date(yyyy,mm-1,dd);
if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == dd ) )
check = true;
else
check = false;
if (yyyy >= 2100)
{
alert("กรุณาระบุรูปแบบวันที่ให้ถูกต้อง (วัน/เดือน/ปี ค.ศ)")
check = false;
}
} else
check = false;
return this.optional(element) || check;
}, "Invalid date format.");
// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
jQuery.validator.addMethod("defaultInvalid", function(value, element) {
return value != element.defaultValue;
}, "");
jQuery.validator.addMethod("billingRequired", function(value, element) {
if ($("#bill_to_co").is(":checked"))
return $(element).parents(".subTable").length;
return !this.optional(element);
}, "");
jQuery.validator.messages.required = "";
jQuery.validator.messages.number = "";
jQuery.validator.messages.datepicker = "";
errors = null;
$("form").validate({
invalidHandler: function(e, validator) {
errors = validator.numberOfInvalids();
errorsCount = validator.numberOfInvalids();
//alert("errorsCount " + errorsCount);
if (errors) {
var message = errors == 1
? 'กรุณากรอกข้อมูลที่ถูกต้องจำนวน 1 ข้อมูล แสดงข้อมูลตามแถบสีด้านล่าง'
: 'กรุณากรอกข้อมูลที่ถูกต้องจำนวน ' + errors + ' ข้อมูล แสดงข้อมูลตามแถบสีด้านล่าง';
$("div.error span").html(message);
$("div.error").show();
} else {
$("div.error").hide();
}
},
onkeyup: false,
// submitHandler: function() {
// //$("div.error").hide();
// //alert('xxxx');
// //this.submit();
// //submit();
// },
messages: {
password2: {
required: " ",
equalTo: "Please enter the same password as above"
},
email: {
required: " ",
email: "Please enter a valid email address, example: you@yourdomain.com",
remote: jQuery.validator.format("{0} is already taken, please enter a different address.")
}
}
});
$(".resize").vjustify();
$("div.buttonSubmit").hoverClass("buttonSubmitHover");
if ($.browser.safari) {
$("body").addClass("safari");
}
$("input.phone").mask("(999) 999-9999");
$("input.zipcode").mask("99999");
$("input.datepicker").mask("99/99/9999");
$("input.bankBranch").mask("9999");
$("input.rcCode").mask("9.99999999999");
var creditcard = $("#creditcard").mask("9999 9999 9999 9999");
$("#cc_type").change(
function() {
switch ($(this).val()){
case 'amex':
creditcard.unmask().mask("9999 999999 99999");
break;
default:
creditcard.unmask().mask("9999 9999 9999 9999");
break;
}
}
);
// toggle optional billing address
var subTableDiv = $("div.subTableDiv");
var toggleCheck = $("input.toggleCheck");
toggleCheck.is(":checked")
? subTableDiv.hide()
: subTableDiv.show();
$("input.toggleCheck").click(function() {
if (this.checked == true) {
subTableDiv.slideUp("medium");
$("form").valid();
} else {
subTableDiv.slideDown("medium");
}
});
});
$.fn.vjustify = function() {
var maxHeight=0;
$(".resize").css("height","auto");
this.each(function(){
if (this.offsetHeight > maxHeight) {
maxHeight = this.offsetHeight;
}
});
this.each(function(){
$(this).height(maxHeight);
if (this.offsetHeight > maxHeight) {
$(this).height((maxHeight-(this.offsetHeight-maxHeight)));
}
});
};
$.fn.hoverClass = function(classname) {
return this.hover(function() {
$(this).addClass(classname);
}, function() {
$(this).removeClass(classname);
});
};