Jquery 기반 태그 스마트 인증 구현 코드

16918 단어 라벨 지능 검증
이후 Jquery 에 대한 학습 을 통 해 Jquery 의 강력 함 은 보조 코드 가 너무 많아 유지 하기 어 려 운 문 제 를 해결 했다.AutoValidate.JS
 
/// <reference path="../Scripts/jquery-1.4.1-vsdoc.js" />
// v1.0, 2010-12-9 2010-12-16 MR.X
// 2010-12-10、2010-12-12、2010-12-15、2010-12-16
// type=text type=checkbox type=radio select
//vld="***"
//vld="n***"
//err="***"
//super="y" <span> , y , super="y" ,
//len="***" , textarea
//<input id="***" type="text" vld="***" err="***" span="***" len="***"/>
$(function () {
//
var strRegex = {};
//
var strError = {};
//
var strRight = {};
/** start **/
//
strRegex.NoNull = /[^\s]+/;
strError.NoNull = " , 123、 !";
//
strRegex.Email = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
strError.Email = " , [email protected]!";
//
strRegex.Url = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^\"\"])*$/;
strError.Url = " , https://www.jb51.net!";
//
strRegex.An = /^([a-zA-Z0-9]|[_]){6,16}$/;
strError.An = " , china_56!";
//
strRegex.Math = /\d+$/;
strError.Math = " , 1234!";
//
strRegex.Age = /^\d{2}$/;
strError.Age = " ,10~99 !";
//
strRegex.Post = /^[1-9]\d{5}$/;
strError.Post = " , 150001!";
//
strRegex.Phone = /^((\d{11})|((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})))$/;
strError.Phone = " , 15546503251!";
//
strRegex.Card = /^(([1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[X,x]))|([1-9]\d{5}[1-9]\d{1}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])(\d{3})))$/;
strError.Card = " , 230103190001010000!";
//
strRegex.Price = /^([1-9]\d*|0)(\.\d+)?$/;
strError.Price = " , 99.98!";
//
strRegex.Date = /((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/;
strError.Date = " , 1999.9.9、1999-9-9、1999.09.09!";
//
strRegex.Time = /^([0-9]|[0-1][0-9]|[2][0-3])(:|:)([0-5][0-9])$/;
strError.Time = " , 23:59!";
strError.Length = " , ";
strRight.Info = " !"; //
//
strRegex.DDL = " ";
strError.DDL = " ";
// checkbox
strRegex.Check = " ";
strError.Check = " ";
// radio
strRegex.Radio = " ";
strError.Radio = " ";
// checkbox
strRegex.CheckGroup = " ";
strError.CheckGroup = " ";
// radio
strRegex.RadioGroup = " ";
strError.RadioGroup = " ";
//
var SpanError = "<span class='vldSpanErr'><img src='" + FilePath() + "images/error.gif' /></span>";
var SpanOk = "<span class='vldSpanRig'><img src='" + FilePath() + "images/ok.gif' /></span>";
/** end **/
/** Main **/
// , ../
function FilePath() {
var file = "";
var path = window.location.pathname.split('/');
$(path).each(function () {
file = "../" + file;
});
return file;
}
//
$("#form1 [vld]").blur(function () {
RegexGether($(this));
});
//
function RegexGether($ctrl) {
switch ($ctrl.attr("vld")) {
case "nonull":
RegexNull($ctrl);
break;
case "age":
RegexInputTextAll($ctrl, strRegex.Age, strError.Age);
break;
case "nage":
RegexInputTextOnly($ctrl, strRegex.Age, strError.Age);
break;
case "date":
RegexInputTextAll($ctrl, strRegex.Date, strError.Date);
break;
case "ndate":
RegexInputTextOnly($ctrl, strRegex.Date, strError.Date);
break;
case "price":
RegexInputTextAll($ctrl, strRegex.Price, strError.Price);
break;
case "nprice":
RegexInputTextOnly($ctrl, strRegex.Price, strError.Price);
break;
case "email":
RegexInputTextAll($ctrl, strRegex.Email, strError.Email);
break;
case "nemail":
RegexInputTextOnly($ctrl, strRegex.Email, strError.Email);
break;
case "post":
RegexInputTextAll($ctrl, strRegex.Post, strError.Post);
break;
case "npost":
RegexInputTextOnly($ctrl, strRegex.Post, strError.Post);
break;
case "card":
RegexInputTextAll($ctrl, strRegex.Card, strError.Card);
break;
case "ncard":
RegexInputTextOnly($ctrl, strRegex.Card, strError.Card);
break;
case "time":
RegexInputTextAll($ctrl, strRegex.Time, strError.Time);
break;
case "ntime":
RegexInputTextOnly($ctrl, strRegex.Time, strError.Time);
break;
case "math":
RegexInputTextAll($ctrl, strRegex.Math, strError.Math);
break;
case "nmath":
RegexInputTextOnly($ctrl, strRegex.Math, strError.Math);
break;
case "url":
RegexInputTextAll($ctrl, strRegex.Url, strError.Url);
break;
case "nurl":
RegexInputTextOnly($ctrl, strRegex.Url, strError.Url);
break;
case "an":
RegexInputTextAll($ctrl, strRegex.An, strError.An);
break;
case "nan":
RegexInputTextOnly($ctrl, strRegex.An, strError.An);
break;
case "phone":
RegexInputTextAll($ctrl, strRegex.Phone, strError.Phone);
break;
case "nphone":
RegexInputTextOnly($ctrl, strRegex.Phone, strError.Phone);
break;
case "ddl":
RegexSelect($ctrl);
break;
case "check":
RegexInputCheckBoxRadioOnly($ctrl, strError.Check);
break;
case "radio":
RegexInputCheckBoxRadioOnly($ctrl, strError.Radio);
break;
case "checkgroup":
RegexInputCheckBoxRadioAll($ctrl, strError.CheckGroup);
break;
case "radiogroup":
RegexInputCheckBoxRadioAll($ctrl, strError.RadioGroup);
break;
}
}
//
function RegexNull($ctrl) {
if (strRegex.NoNull.test($ctrl.val())) {
return RegexLen($ctrl);
}
else {
Error($ctrl, strError.NoNull);
return false;
}
}
// input(type=radio) input(type=checkbox)
function RegexInputCheckBoxRadioAll($ctrl, error) {
if ($ctrl.parent().children(":checked").length == 0) {
if ($ctrl.parent().children().attr("super")) {
// [super='y'] , , return false; ,
$ctrl.parent().children("[super='y']").each(function () {
Error($(this), error);
return false;
});
}
else {
//
$ctrl.parent().children("[type='" + $ctrl.attr("type") + "']").each(function () {
Error($(this), error);
});
}
return false;
}
else {
if ($ctrl.parent().children().attr("super")) {
$ctrl.parent().children("[super='y']").each(function () {
Ok($(this));
return false;
});
}
else {
$ctrl.parent().children().each(function () {
Ok($(this));
});
}
return true;
}
}
// input(type=radio) input(type=checkbox)
function RegexInputCheckBoxRadioOnly($ctrl, error) {
if (!$ctrl.attr("checked")) {
Error($ctrl, error);
return false;
}
else {
Ok($ctrl);
return true;
}
}
//select ,
function RegexSelect($ctrl) {
if ($ctrl.val() == strRegex.DDL) {
Error($ctrl, strError.DDL);
return false;
}
else {
Ok($ctrl);
return true;
}
}
// ,type=text
function RegexInputTextOnly($ctrl, Regex, error) {
//
if (strRegex.NoNull.test($ctrl.val())) {
return RegexOtherFormat($ctrl, Regex, error);
}
else {
Ok($ctrl);
return true;
}
}
// ,type=text
function RegexInputTextAll($ctrl, Regex, error) {
//
if (strRegex.NoNull.test($ctrl.val())) {
return RegexOtherFormat($ctrl, Regex, error);
}
else {
Error($ctrl, error);
return false;
}
}
//
function RegexOtherFormat($ctrl, Regex, error) {
if (Regex.test($ctrl.val())) {
return RegexLen($ctrl);
}
else {
Error($ctrl, error);
return false;
}
}
//textarea
function RegexLen($ctrl) {
// len
if ($ctrl.attr("len")) {
var error = strError.Length + $ctrl.attr("len") + " !";
if (parseInt($ctrl.val().length) > parseInt($ctrl.attr("len"))) {
Error($ctrl, error);
return false;
}
}
Ok($ctrl);
return true;
}
//
function Error($ctrl, error) {
// err , title
if ($ctrl.attr("err")) {
error = $ctrl.attr("err");
}
if ($ctrl.attr("super")) {
// super span
$ctrl.parent().find("span").remove();
$ctrl.parent().append(SpanError);
$ctrl.parent().find("span").append(error);
$ctrl.parent().find("span").fadeTo("slow", 0.66);
}
else {
// title
$ctrl.attr("title", error);
$ctrl.addClass("error");
}
}
// ,
function Ok($ctrl) {
//
if ($ctrl.attr("super")) {
$ctrl.parent().find("span").remove();
}
else {
$ctrl.removeClass("error");
}
if ($.trim($ctrl.val()).length > 0) {
var right = strRight.Info;
// rig
if ($ctrl.attr("rig")) {
right = $ctrl.attr("rig");
}
//
if ($ctrl.attr("super")) {
$ctrl.parent().append(SpanOk);
$ctrl.parent().find("span").append(right);
$ctrl.parent().find("span").fadeTo("slow",0.99).fadeTo("slow",0.99).fadeTo("slow",0);
}
else {
$ctrl.attr("title", right);
}
}
}
//
$("#vldBtn").click(function () {
$("#form1 [vld]").each(function () {
RegexGether($(this));
});
// error
if ($("#form1 [vld][class='error']").length > 0) {
return false;
}
else {
return true;
}
});
});
AutoValidate.htm
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="Css/style01.css" rel="stylesheet" type="text/css" />
<link href="Css/AutoValidate.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/Jquery.timer.js" type="text/javascript"></script>
<script src="Js/style.js" type="text/javascript"></script>
<script src="Js/AutoValidate.js" type="text/javascript"></script>
</head>
<body>
<form id="form1">
<ul class="tab_ul">
<li>
<p> :</p>
<p><input id="name" type="text" vld="nonull" err="okokok!!!" super="y" rig=" a" /></p>
</li>
<li>
<p> :</p>
<p><input id="age" type="text" vld="age" /></p>
</li>
<li>
<p> :</p>
<p><input id="date" type="text" vld="ndate" /></p>
</li>
<li>
<p> :</p>
<p><input id="price" type="text" vld="nprice" /></p>
</li>
<li>
<p> :</p>
<p><input id="email" type="text" vld="email" /></p>
</li>
<li>
<p> :</p>
<p><input id="post" type="text" vld="post" /></p>
</li>
<li>
<p> :</p>
<p><input id="phone" type="text" vld="phone" len="10" /></p>
</li>
<li>
<p> :</p>
<p><input id="card" type="text" vld="card" /></p>
</li>
<li>
<p> :</p>
<p><input id="time" type="text" vld="ntime" /></p>
</li>
<li>
<p> :</p>
<p><input id="math" type="text" vld="nmath" /></p>
</li>
<li>
<p> :</p>
<p><input id="url" type="text" vld="url" /></p>
</li>
<li>
<p> :</p>
<p><input id="an" type="text" vld="nan" /></p>
</li>
<li style="height:60px">
<p> :</p>
<p><textarea id="len" style="width:300px; height:50px;" len="50" vld="nonull"></textarea></p>
</li>
<li>
<p> :</p>
<p><select id="ddl" vld="ddl"><option> </option><option value="1"> </option></select></p>
</li>
<li>
<p> :</p>
<p><input type="checkbox" id="check1" vld="check" value="1" /><label for="check1"> </label></p>
</li>
<li>
<p> :</p>
<p><input type="radio" id="radio1" vld="radio" value="1" /><label for="radio1"> </label></p>
</li>
<li>
<p> :</p>
<p><input type="checkbox" id="Checkbox1" vld="checkgroup" name="dsa" value="1" err=" " super="y"/><label for="Checkbox1"> </label><input type="checkbox" id="Checkbox2" name="dsa" vld="checkgroup" value="1" /><label for="Checkbox2"> </label></p>
</li>
<li>
<p> :</p>
<p><input type="radio" id="radio2" vld="radiogroup" name="asd" value="1" /><label for="radio2"> </label><input type="radio" id="radio3" name="asd" vld="radiogroup" value="1" /><label for="radio3"> </label></p>
</li>
<li>
<input id="vldBtn" type="button" value=" " />
</li>
</ul>
</form>
</body>
</html>

좋은 웹페이지 즐겨찾기