Mindon.IDEA

Air off, Mind on ~ / Javascript+Golang, Sci, Health… /

Javascript之校验脚本

BlogMS original blog key: 1000124485, blog id: airoff History stat: 浏览/评论:234/2 , 日期:2004年12月22日 17:19

/* value validity relative javascript

*/

var g_valid_func = new ValidFUNC();

var valid_error_message = "";

var INVALID_USER_NAME = /\|\/|[|]|:|||<|>|+|\=|\;|\,|\?|*|&|\%|`|^|.|"| /;

var INVALID_FILE_NAME = /\|\/|:|*|"|<|>||/;

var REXP_DIGITAL = /^\d+$/;

var REXP_BLANK = /^\s*$/;

var TIME_FORMAT = "hh:mm:ss";

var TIME_RANGE = "hh:00~23, mm:00~59, ss:00~59";

var DATE_FORMAT = "YYYY-MM-DD";

var DATE_RANGE = "YYYY:0000~9999, MM:01~12, DD:01~28/29/30/31";

var R_GE = "大于等于 ";

var R_LE = "小于等于 ";

var R_EQ = "等于 ";

var R_GT = "大于 ";

var R_LT = "小于 ";

var R_AND = "并且";

var EMPTY_ERRM = "不能为空";

var NONE_SELECTED_ERRM = "请选择";

var INVALID_USER_NAME_ERRM = "不能包含以下字符:\n| [ ] : < > + = ; , ? * / \ & % ` ^ . &quot; 空格";

var INVALUD_FILE_NAME_ERRM = "不能包含以下字符:\n| \ / : * &quot; < >";

var EXCLUDED_ERRM = "不能取以下值:\n";

var NOT_INCLUDED_ERRM = "只能取以下值:\n";

var NOT_LENGTH_ERRM = "不符合串长度限制: ";

var NOT_MAIL_ERRM = "不符合电子邮箱格式: 需含@,且@前后至少各一字母";

var NOT_RANGE_ERRM = "不符合串值范围限制: ";

var DNOT_RANGE_ERRM = "不符合数字值范围限制: ";

var DNOT_HEX_ERRM = "不符合十六进制数规格(0~9, A~F 或 a~f构成)";

var DNOT_LENGTH_ERRM = "不符合数字串长度限制: ";

var DNOT_TIME_ERRM = "不符合有效时间格式: ";

var DNOT_DATE_ERRM = "不符合有效日期格式: ";

function validERR(field, label)

// alert valid error message and return false

// @param :field, field handle id

// @param :label, field label

// @return :false

{

var field, label;

var params = g_valid_func.params;

var message = "&quot;" +label +"&quot; ";

switch(g_valid_func.id) {

case 50000: //gEmpty

message += EMPTY_ERRM;

break;

case 50010: //gNotUserName

message += INVALID_USER_NAME_ERRM;

break;

case 50020: //gNotFileName

message += INVALID_FILE_NAME_ERRM;

break;

case 50030: //gExcluded

message += EXCLUDED_ERRM + params[1].join(", ");

break;

case 50040: //gNotFilter

message += NOT_INCLUDED_ERRM + params[1].join(", ");

break;

case 50050: //gNotLen

message += NOT_LENGTH_ERRM;

if(params[1] == "0" || params[1]) message += R_GE +params[1];

if(params[2] == "0" || params[2]) message += ", " +R_LE +params[2];

break;

case 50060: //gNotMail

message += NOT_MAIL_ERRM;

break;

case 50070: //gNotFit

message += NOT_RANGE_ERRM;

if(params[1] == "0" || params[1]) message += R_GE +params[1];

if(params[2] == "0" || params[2]) message += ", " +R_LE +params[2];

break;

case 60010: //dNotFit

message += DNOT_RANGE_ERRM;

if(params[1] == "0" || params[1]) message += R_GE +params[1];

if(params[2] == "0" || params[2]) message += ", " +R_LE +params[2];

break;

case 60020: //dNotHex

message += DNOT_HEX_ERRM;

break;

case 60030: //dNotLen

message += DNOT_LENGTH_ERRM;

if(params[1] == "0" || params[1]) message += R_GE +params[1];

if(params[2] == "0" || params[2]) message += ", " +R_LE +params[2];

break;

case 60040: //dNotTime

message += DNOT_TIME_ERRM +TIME_FORMAT +"\n(" +TIME_RANGE + ")";

break;

case 60050: //dNotDate

message += DNOT_DATE_ERRM;

message += DATE_FORMAT.substr(params[1]?2:0) +"\n(" +DATE_RANGE.substr(params[1]?2:0) + ")";

break;

case 60060: //dNotDateTime

message += DNOT_TIME_ERRM;

message += DATE_FORMAT.substr(params[2]?2:0) +(params[1]?params[1]:"") +TIME_FORMAT;

message += "\n(" +DATE_RANGE.substr(params[2]?2:0) +")\n(" +TIME_RANGE +")";

break;

case 90000: //gNone

message = NONE_SELECTED_ERRM +message;

break;

}

alert(message);

try{ field.focus() } catch(e) {};

return false;

}

function ValidFUNC()

// valid special character parameters

{

this.id = "";

this.name = "";

this.description = "";

this.params = [];

this.set = set;

function set(id, params, name, description)

{

var id, params, name, description;

if(id) this.id = id;

if(params) this.params = params;

if(name) this.name = name;

if(description) this.description = description;

}

}

function s2d(v) { var n = new Number(v); return parseInt(isNaN(n)?0:n); }

function contains(varr, value)

// judge if a value is contained in a array

// @param :varr, value array

// @param :value, value string

// @return :if value is in array, return true, else return false

{

var arrstr = varr.join(",");

if(arrstr.indexOf(",") < 0) return (arrstr == value);

var index = arrstr.indexOf(value.toString());

var offset = value.toString().length;

var hok = (index == 0 || arrstr.substr(index -1, 1) == ",");

var tok = (index == (arrstr.length - offset) || arrstr.substr(index +offset, 1) == ",");

return (index>=0 && hok && tok);

}

function gEmpty(value)

// check if the value only contains blank character

// @param :value, value being checked

// @return :if only contains BLANK-charaters return true, else false

{

var value;

g_valid_func.set(50000, arguments, "gEmpty", "function gEmpty(value)");

if(!value && value != "0") return true;

return REXP_BLANK.test(value);

}

function gNotUserName(value)

// check if the name string is invalid

// @param :value, value being checked

// @return :if name string invalid return true, else false

{

var value;

g_valid_func.set(50010, arguments, "gNotUserName", "function gNotUserName(value)");

if(!value) return true;

return INVALID_USER_NAME.test(value);

}

function gNotFileName(value)

// check if the filename string is invalid

// @param :value, value being checked

// @return :if filename string invalid return true, else false

{

var value;

g_valid_func.set(50020, arguments, "gNotFileName", "function gNotFileName(value)");

if(!value) return true;

return INVALID_FILE_NAME.test(value);

}

function gExcluded(value, filter)

// check if the value is in the EXCLUDED-filter-array

// @param :value, value being checked

// @param :filter, excluded-filter-array

// @return :if EXCLUDED return true, else false

{

var value, filter;

g_valid_func.set(50030, arguments, "gExcluded", "function gExcluded(value, filter)");

if(!filter || !filter.length) return false;

return(contains(filter, value));

}

function gNotFilter(value, filter)

// check if the value is NOT in the included-filter-array

// @param :value, value being checked

// @param :filter, included-filter-array

// @return :if NOT included return true, else false

{

var value, filter;

g_valid_func.set(50040, arguments, "gNotFilter", "function gNotFilter(value, filter)");

if(!filter || !filter.length) return false;

return(!contains(filter, value));

}

function gNotLen(value, sLmin, sLmax)

// check if the string value length is NOT between sLmin and sLmax

// @param :value, value being checked

// @param :sLmin, the min limitation of string length

// @param :sLmax, the max limitation of string length

// @return :if NOT fits the length limitation return true, else false

{

var value, sLmin, sLmax;

g_valid_func.set(50050, arguments, "gNotLen", "function gNotLen(value, sLmin, sLmax)");

var flag = false;

if(!value && value != "0" && value != "") return !flag;

if(sLmin || sLmin == "0") {

flag = (value.length >= sLmin);

if(sLmax || sLmax == "0") {

flag = (flag && value.length <= sLmax);

}

} else {

flag = true;

}

return !flag;

}

function gNotMail(value)

// check if the value is NOT a valid email address

// @param :value, value being checked

// @return :if NOT a valid email return true, else false

{

var value;

g_valid_func.set(50060, arguments, "gNotMail", "function gNotMail(value)");

var rexp = /^[^@]+@[^@]+$/g;

var flag = false;

var idx = value.indexOf("@");

if(idx > 0) {

flag = (!gNotUserName(value.substring(0, idx)) && rexp.test(value));

}

return !flag;

}

function gNotFit(value, smin, smax)

// check if the string value is NOT between smin and smax

// @param :value, value being checked

// @param :smin, the min string value limitation

// @param :smax, the max string value limitation

// @return :if NOT fits the string value limitation return true, else false;

{

var value, smin, smax;

g_valid_func.set(50070, arguments, "gNotFit", "function gNotFit(value, smin, smax)");

var flag = false;

if(!value && value != "0" && value !="")

return !flag;

if(smin || smin == "0" || smin =="")

flag = (value >= smin);

if(smax || smax == "0" || smax =="")

flag = (flag && value <= dmax);

return !flag;

}

function dNotInt(value, type)

// check if the digital value is NOT proper type of integer

// @param :value, value being checked

// @return :if NOT proper int type return true, else false;

{

var value, type;

if(!type) type = "int";

switch(type.toLowerCase()) {

case "int":

case "short":

return dNotFit(value, -32768, 32767);

break;

case "long":

return dNotFit(value, -2147483648, 2147483647);

break;

case "ushort":

return dNotFit(value, 0, 65535);

break;

case "ulong":

return dNotFit(value, 0, 4294967295);

break;

}

return true;

}

function dNotFit(value, dmin, dmax)

// check if the digital value is NOT between dmin and dmax

// @param :value, value being checked

// @param :dmin, the min value limitation

// @param :dmax, the max value limitation

// @return :if NOT fits the value limitation return true, else false;

{

var value, dmin, dmax;

g_valid_func.set(60010, arguments, "dNotFit", "function dNotFit(value, dmin, dmax)");

var flag = false;

if(!value && value != "0") return !flag;

flag = !isNaN(value);

if(!dmin && dmin != "0") return !flag;

flag = (flag && value >= dmin);

if(!dmax && dmax != "0") return !flag;

flag = (flag && value <= dmax);

return !flag;

}

function dNotHex(value)

// check if the digital value is NOT hex value

// @param :value, value being checked

// @return :if NOT hex value return true, else false;

{

var value;

g_valid_func.set(60020, arguments, "dNotHex", "function dNotHex(value)");

var rexp = /^[0-9a-fA-F]+$/;

return !rexp.test(value);

}

function dNotLen(value, dLmin, dLmax)

// check if the digital string value length is NOT between dLmin and dLmax

// @param :value, value being checked

// @param :dLmin, the min limitation of string length

// @param :dLmax, the max limitation of string length

// @return :if NOT fits the length limitation or contains NOT-digital-characters return true, else false;

{

var value, dLmin, dLmax;

g_valid_func.set(60030, arguments, "dNotLen", "function dNotLen(value, dLmin, dLmax)");

var flag = false;

if(!value && value != "0") return !flag;

var rexpstr = "^\d";

if((!dLmin && dLmin != "0") || isNaN(dLmin)) {

rexpstr += "+$"

} else {

rexpstr += "{" +dLmin +",";

if((!dLmax && dLmax != "0") || isNaN(dLmax)) {

rexpstr += "}$"

} else {

rexpstr += dLmax +"}$";

}

}

var rexp = new RegExp(rexpstr);

flag = rexp.test(value);

return !flag;

}

function dNotTime(value)

// check if the value is NOT a time string

// @param :value, value being checked

// @return :if NOT time string return true, else return false

{

var value;

g_valid_func.set(60040, arguments, "dNotTime", "function dNotTime(value)");

var rexp = /[^\d]+/; // detect delimiter string

if(!value) return true;

var delimiter = value.match(rexp);

if(delimiter) {

delimiter = delimiter.toString();

var dlen = delimiter.length;

if(value.indexOf(delimiter) != 2) return true;

value = value.substr(0,2) + value.substr(2 +dlen, 2) + value.substr(4 +2*dlen, 2);

}

var tarr = [];

for(var k=0; k<3; k++) {

tarr[k] = value.substr(2*k, 2);

if(isNaN(tarr[k])) return true;

tarr[k] = s2d(tarr[k]);

}

if(tarr[0] < 0 || tarr[0] >= 24

|| tarr[1] < 0 || tarr[1] >= 60

|| tarr[2] < 0 || tarr[2] >= 60) {

return true;

}

return false;

}

function dNotDate(value, y2d)

// check if the value is NOT a date string

// @param :value, value being checked

// @param :y2d, two digital year flag

// @return :if NOT date string return true, else return false

{

var value, y2d;

g_valid_func.set(60050, arguments, "dNotDate", "function dNotDate(value, y2d)");

var rexp = /[^\d]+/; // detect delimiter string

if(!value) return true;

var delimiter = value.match(rexp);

var YRW = 4;

if(y2d) YRW = 2;

if(delimiter) {

delimiter = delimiter.toString();

var dlen = delimiter.length;

if(value.indexOf(delimiter) != YRW) return true;

value = value.substr(0,YRW) + value.substr(YRW +dlen, 2) + value.substr(YRW +2 +2*dlen, 2);

}

var tarr = [];

try {

for(var k=0; k<3; k++) {

tarr[k] = value.substr((k==0?0:(YRW +2*(k-1))), (k==0?YRW:2));

tarr[k] = new Number(tarr[k]);

//tarr[k] = s2d(tarr[k]);

if(y2d && k==0) tarr[k] = 1900 +tarr[k]; //year 2 digital process

}

} catch(e) { return true; }

var cd = new Date(tarr[0], tarr[1] -1, tarr[2]);

var dfit = (cd.getFullYear() == tarr[0]);

dfit = (dfit && cd.getMonth() == tarr[1] -1);

dfit = (dfit && cd.getDate() == tarr[2]);

if(!dfit) return true;

/*//

var febdays = ((tarr[0]%4 == 0) && (tarr[0]%100 != 0) || (tarr[0]%400 == 0))?29:28;

var mdarr = new Array(31,febdays,31,30,31,30,31,31,30,31,30,31);

if(tarr[0] < 0

|| tarr[1] < 1 || tarr[1] > 12

|| tarr[2] < 1 || tarr[2] > mdarr[tarr[1] -1]) {

return true;

}

//*/

return false;

}

function dNotDateTime(value, delimiter, y2d)

// check if the value is NOT a datetime string

// @param :value, value being checked

// @param :delimiter, delimiter between date string and time string

// @param :y2d, two digital year flag

// @return :if NOT datetime string return true, else return false

{

var value, delimiter, y2d;

g_valid_func.set(60060, arguments, "dNotDateTime", "function dNotDateTime(value, delimiter, y2d)");

if(!value) return true;

if(!delimiter) delimiter = "";

var datestr, timestr;

var DTW = 8;

if(y2d) DTW = 6;

if(!delimiter) {

datestr = value.substr(0, DTW);

timestr = value.substr(DTW, 6);

} else {

var dpos = value.indexOf(delimiter);

datestr = value.substring(0, dpos);

timestr = value.substring(dpos +delimiter.length, value.length);

}

return (dNotDate(datestr, y2d) || dNotTime(timestr));

}

function gNone(field)

// check if value has been set by selection

// @param :field, field to be check

{

var field;

g_valid_func.set(90000, arguments, "gNone", "function gNone(field)");

if(field.type && field.type == "select-one" && field.value=="") return true;

if(field.type && field.type == "checkbox") return (field.checked?false:true);

if(!field.type && field.length) {

for(var k=0; k<field.length; k++) {

if(field[k].checked) return false;

}

return true;

}

return false;

}

Comments