验证中...
Languages: JavaScript
Categories: JavaScript 工具
Latest update 2019-06-12 13:43
validators
Raw Copy
/* ref: https://github.com/WLDragon/SMValidator/wiki/API%E8%AF%B4%E6%98%8E */
// depends on SMValidator
// REG_EXP
var __GLOBAL_RE_VALIDATE = {
phone: {
reg: /^1\d{10}$/,
tip: '请输入正确的手机号',
},
};
/**
* deep copy Object
* @param sourceObj {Object}
* @param targetObj {Object}
*/
function deepCopy(sourceObj, targetObj) {
if (Object.prototype.toString.call(sourceObj) === '[object object]'
&& Object.prototype.toString.call(targetObj) === '[object object]') {
for (var key in sourceObj) {
if (sourceObj.hasOwnProperty(key)) {
targetObj[key] = sourceObj[key];
}
}
}
}
/**
* update __GLOBAL_RE_VALIDATE
* @param validators {Object}
*/
function updateGlobalReValidate(validators) {
// copy validators to __GLOBAL_RE_VALIDATE
deepCopy(validators, __GLOBAL_RE_VALIDATE)
}
/**
* mount to DOM
* @param mountNode {Selector}
* @param delay {Number}
* @param validators {Object}
*/
function mountSMValidator(mountNode, delay, validators) {
delay = delay || 0;
var RE_VALIDATE = {};
// copy __GLOBAL_RE_VALIDATE to RE_VALIDATE
deepCopy(__GLOBAL_RE_VALIDATE, RE_VALIDATE);
// copy validators to RE_VALIDATE, and this will overwrite __GLOBAL_RE_VALIDATE
deepCopy(validators, RE_VALIDATE);
var __SMValidatorConf = {
required: '这是必填项',
submit: function(valid, form) {
if (valid) {
form.submit();
}
},
rules: {
// phone: [RE_VALIDATE.phone.reg, RE_VALIDATE.phone.tip,],
},
fields: {
// 'smv-phone': {rule: 'phone', required: true,},
},
};
for (var key in RE_VALIDATE) {
if (RE_VALIDATE.hasOwnProperty(key)) {
__SMValidatorConf.rules[key] = [RE_VALIDATE[key].name, RE_VALIDATE[key].tip,];
__SMValidatorConf.fields['smv-' + key] = {rule: key, required: true,};
}
}
setTimeout(function() {
new SMValidator(mountNode, __SMValidatorConf);
}, delay);
}
/**
* set language
*/
function setSMValidatorLang() {
SMValidator.setLang({
number: '请输入数字',
email: '请输入正确的邮箱格式',
/* eslint-disable camelcase */
range_equal: '值必须等于{0}',
range_scope: '值必须大于{0}且小于{1}',
range_greater: '值必须大于{0}',
range_less: '值必须小于{0}',
range_no_number: '值必须是数字',
error_param: '参数错误',
length_equal: '长度必须为{0}',
length_scope: '长度必须大于{0}且小于{1}',
length_greater: '长度必须大于{0}',
length_less: '长度必须小于{0}',
password: '两次输入密码不一致',
/* eslint-enable */
});
}
/**
* set skin
* @param skinName {String}
*/
function setSMValidatorSkin(skinName) {
var __SMValidatorSkins = {
primary: {
failStyle: {'border-color': 'red',},
// eslint-disable-next-line max-len
failHtml: '<p style="position:absolute;padding:3px;font-size:.75em;color:red;z-index:100;"></p>',
// failCss: '',
passStyle: {'border-color': 'green',},
// passHtml: '',
// passCss: '',
},
};
__SMValidatorSkins[skinName] && SMValidator.config(__SMValidatorSkins[skinName]);
}
/**
* init
* @param mountNode {Selector}
* @param delay {Number}
* @param validators {Object}
* @param skinName {String}
*/
function initSMValidator(mountNode, delay, validators, skinName) {
mountNode = mountNode || 'form';
skinName = skinName || 'primary';
setSMValidatorLang();
setSMValidatorSkin(skinName);
mountSMValidator(mountNode, delay, validators);
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_sixth 5th_float_left_close