Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get element value use jQuery#val() #154

Merged
merged 2 commits into from
Aug 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ Thumbs.db
src/libs
src/test.html
src/index.html

.idea
*.iml
2 changes: 1 addition & 1 deletion jquery.validator.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions local/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
,url: [/^(https?|s?ftp):\/\/\S+$/i, "Please enter a valid URL."]
,accept: function (element, params){
if (!params) return true;
var ext = params[0];
var ext = params[0],
value = $(element).val();
return (ext === '*') ||
(new RegExp(".(?:" + ext + ")$", "i")).test(element.value) ||
(new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
this.renderMsg("Only accept {1} file extension.", ext.replace(/\|/g, ', '));
}

Expand Down
5 changes: 3 additions & 2 deletions local/ja.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
,url: [/^(https?|s?ftp):\/\/\S+$/i, "有効なURLを入力してください"]
,accept: function (element, params){
if (!params) return true;
var ext = params[0];
var ext = params[0],
value = $(element).val();
return (ext === '*') ||
(new RegExp(".(?:" + ext + ")$", "i")).test(element.value) ||
(new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
this.renderMsg("ファイル拡張子を{1}のみを受け入れる", ext.replace(/\|/g, '、'));
}

Expand Down
5 changes: 3 additions & 2 deletions local/zh_CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
,password: [/^[\S]{6,16}$/, "请输入6-16位字符,不能包含空格"]
,accept: function (element, params){
if (!params) return true;
var ext = params[0];
var ext = params[0],
value = $(element).val();
return (ext === '*') ||
(new RegExp(".(?:" + ext + ")$", "i")).test(element.value) ||
(new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
this.renderMsg("只接受{1}后缀的文件", ext.replace(/\|/g, ','));
}

Expand Down
5 changes: 3 additions & 2 deletions local/zh_TW.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
,url: [/^(https?|s?ftp):\/\/\S+$/i, "請輸入有效的網址"]
,accept: function (element, params){
if (!params) return true;
var ext = params[0];
var ext = params[0],
value = $(element).val();
return (ext === '*') ||
(new RegExp(".(?:" + ext + ")$", "i")).test(element.value) ||
(new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
this.renderMsg("只接受{1}後綴的文件", ext.replace(/\|/g, ','));
}

Expand Down
36 changes: 20 additions & 16 deletions src/jquery.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
return el.getAttribute(key);
}
},
elementValue = function(element) {
return $(element).val();
},
debug = window.console || {
log: noop,
info: noop
Expand Down Expand Up @@ -256,7 +259,7 @@

// any value, but not only whitespace
$.expr[":"].filled = function(elem) {
return !!trim(elem.value);
return !!trim(elementValue(elem));
};


Expand Down Expand Up @@ -636,7 +639,7 @@
};

// only gets focus, no verification
if (key === 9 && !el.value) return;
if (key === 9 && !elementValue(el)) return;

// do not validate, if triggered by these keys
if (key < 48 && !specialKey[key]) return;
Expand Down Expand Up @@ -695,7 +698,7 @@
me.elements[field.key] = ret.element = el;
me.$el[0].isValid = isValid ? me.isFormValid() : isValid;

field.old.value = el.value;
field.old.value = elementValue(el);
field.old.id = el.id;
field.old.ret = ret;

Expand Down Expand Up @@ -825,6 +828,7 @@
key = field.key,
rule = field.rules[field._i],
method = rule.method,
value = elementValue(el),
params = rule.params;

// request has been sent, wait it
Expand All @@ -834,7 +838,7 @@

if ( !field.must && rule.ret !== undefined &&
old.rule === rule && old.id === el.id &&
el.value && old.value === el.value )
value && old.value === value )
{
// get result from cache
ret = rule.ret;
Expand Down Expand Up @@ -911,7 +915,7 @@
if (me.options.debug) debug.info(field.key);

// if the field is not required, and that has a blank value
if (!field.required && !field.must && !el.value) {
if (!field.required && !field.must && !elementValue(el)) {
if (!checkable(el)) {
me._validatedField(el, field, {isValid: true});
return;
Expand Down Expand Up @@ -1217,11 +1221,11 @@
return fn;
case 'array':
return function(el) {
return fn[0].test(el.value) || fn[1] || false;
return fn[0].test(elementValue(el)) || fn[1] || false;
};
case 'regexp':
return function(el) {
return fn.test(el.value);
return fn.test(elementValue(el));
};
}
}
Expand Down Expand Up @@ -1378,7 +1382,7 @@
*/
required: function(element, params, field) {
var me = this,
val = trim(element.value),
val = trim(elementValue(element)),
isValid = true;

if (params) {
Expand Down Expand Up @@ -1408,7 +1412,7 @@
ret;

isValid = $elements.filter(function(){
return !!trim(this.value);
return !!trim(elementValue(this));
}).length >= (params[2] || 1);

if (isValid) {
Expand Down Expand Up @@ -1463,7 +1467,7 @@
}
re = '^(?:' + re + ')$';

return new RegExp(re).test(element.value) || this.messages.integer[key];
return new RegExp(re).test(elementValue(element)) || this.messages.integer[key];
},

/** match another field
Expand Down Expand Up @@ -1496,8 +1500,8 @@
// If the compared field is not exist
if (!elem2) return;
field2 = me.getField(elem2);
a = element.value;
b = elem2.value;
a = elementValue(element);
b = elementValue(elem2);

if (!field._match) {
me.$el.on('valid'+CLS_NS_FIELD+CLS_NS, selector2, function(){
Expand Down Expand Up @@ -1551,7 +1555,7 @@
range[~100] Number less than or equal to 100
**/
range: function(element, params) {
return this.getRangeMsg(+element.value, params, 'range');
return this.getRangeMsg(+elementValue(element), params, 'range');
},

/** how many checkbox or radio inputs that checked
Expand Down Expand Up @@ -1589,7 +1593,7 @@
length[~16, true] Less than 16 characters, non-ASCII characters calculating two-character
**/
length: function(element, params) {
var value = element.value,
var value = elementValue(element),
len = (params[1] ? value.replace(rDoubleBytes, 'xx') : value).length;

return this.getRangeMsg(len, params, 'length', (params[1] ? '_2' : ''));
Expand All @@ -1616,7 +1620,7 @@
arr = rAjaxType.exec(params[0]),
data = {};

data[element.name] = element.value;
data[element.name] = elementValue(element);
// There are extra fields
if (params[1]) {
$.map(params.slice(1), function(name) {
Expand All @@ -1642,7 +1646,7 @@
* filter(regexp) filter the "regexp" matched characters
*/
filter: function(element, params) {
element.value = element.value.replace( params ? (new RegExp("[" + params[0] + "]", "gm")) : rUnsafe, '' );
element.value = elementValue(element).replace( params ? (new RegExp("[" + params[0] + "]", "gm")) : rUnsafe, '' );
}
});

Expand Down
5 changes: 3 additions & 2 deletions src/local/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ exports.rules = {
//Acceptable extension, eg. accept(png|jpg|bmp|gif);
accept: function(element, params){
if (!params) return true;
var ext = params[0];
var ext = params[0],
value = $(element).val();
return (ext === '*') ||
(new RegExp(".(?:" + ext + ")$", "i")).test(element.value) ||
(new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
this.renderMsg("Only accept {1} file extension.", ext.replace(/\|/g, ', '));
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/local/ja.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ exports.rules = {
//許容可能なファイル拡張子,など:accept(png|jpg|bmp|gif);
accept: function(element, params){
if (!params) return true;
var ext = params[0];
var ext = params[0],
value = $(element).val();
return (ext === '*') ||
(new RegExp(".(?:" + ext + ")$", "i")).test(element.value) ||
(new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
this.renderMsg("ファイル拡張子を{1}のみを受け入れる", ext.replace(/\|/g, '、'));
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/local/zh_CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ exports.rules = {
//可接受的后缀名,例如:accept(png|jpg|bmp|gif);
accept: function(element, params){
if (!params) return true;
var ext = params[0];
var ext = params[0],
value = $(element).val();
return (ext === '*') ||
(new RegExp(".(?:" + ext + ")$", "i")).test(element.value) ||
(new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
this.renderMsg("只接受{1}后缀的文件", ext.replace(/\|/g, ','));
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/local/zh_TW.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ exports.rules = {
//可接受的後綴名,例如:accept(png|jpg|bmp|gif);
accept: function(element, params){
if (!params) return true;
var ext = params[0];
var ext = params[0],
value = $(element).val();
return (ext === '*') ||
(new RegExp(".(?:" + ext + ")$", "i")).test(element.value) ||
(new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
this.renderMsg("只接受{1}後綴的文件", ext.replace(/\|/g, ','));
}
};
Expand Down