Write simple front-end web development tutorial page form validation procedures

Read Hermit form validation, and read some other verification procedures and related reference materials, write a relatively simple js form validation procedures.

Function Description :

verification:

Feature

Writing ideas :

Simple structure and the entire process, in order to verify the number of common formats, firstly js built-in objects is extended. such as:

String.prototype.isUrl = function(){
var url = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
var tmpStr = this;
return url.test(tmpStr);
}

This is used to verify the http address.

Then write an object called vform contains the main function, vform initialization time, will add the good check validation rules, and to validate the form control object extensions, add validate () and validlength () two functions, and adding onblur events for verification.
Method for establishing div object used error message, the error is displayed correctly hidden. So also you need to define a style for div.
Use before you need to specify the form id id attention is not the name or else be wrong, but when added to the rules specified by the name attribute is not id form controls

Parameter validation rules:

obj - name form controls

minLength - fill in the minimum length of a string of 0 means that you can not fill and 1 means that will add

dataType - validation format

maxLength - the maximum length.

And other programs are not the same: I am treated differently by the http address and date format of the address, the address does not fill http: // I will add after verification, this is more humane, in line with availability requirements. The process is the same date, as much as possible to fill the first digital format conversion, and then verify. Format using yyyy-mm-dd format.

Create a front-end learning qun438905713, most in the group are zero-based learners, we help each other, answer each other, and also to prepare a lot of learning materials, welcomed the zero-based junior partner to the exchange. The original application file a calendar input control is omitted here. . .

Total code :

Operation code box
 *

vForm1.0beta

  • Author: Lei Xiao Bao
  • Time: 2006-08-08
  • URL: http: //lxbzj.com
  • e-mail: [email protected]
  • License: LGPL

Function Description:

  1. verification:

    • http address.
    • The date and time
    • e-mail
    • digital
    • Character length check
    • An input and another input of the comparator (for example: Make sure to enter a password)
    • Size comparison (only a relatively symbol)
  2. Feature

    • Easy expansion, you can easily add their own needs authentication
    • Compatibility (ie5,6 firefox, oprea).
    • The availability of good, did not use alert () to pop-up prompts;

 

Instructions

In use, the need to define a style error message box, the present embodiment is the pattern:div.info { width: 170px;
overflow:visible;
height:auto;
font-size: small;
position: absolute;
background-color: #FFffdd;
border: 1px solid #000;
filter:progid:DXImageTransform.Microsoft.Shadow(color=#111111,direction=135,strength=3);
padding: 5px;
}

Then in the page <head> section add <script type="text/javascript" src="calendar/calendar.js"></script>
, then you can write a function to set the form name, validation rules, and finally add the onload event for the body. function start()

vFormvform.form_id = 'form1';
vform.err_class = 'info';
// (obj,required(true/false),dataType,errmsg,minlen,maxlen,rule,patams)
//验证规则,逐条填写
vform.rules.add('frm_name',1,'e-mail','请您按照 [email protected] 的格式输入电子邮件地址。<br /><span style="color:#f00">必填项目</span>');
vform.rules.add('myweb',1,'url','请您按照 http://www.domain.com 的格式输入您的网址。<br /><span style="color:#f00">必填项目</span>');
vform.rules.add('dateinput',0,'date','请按2000-03-05 的格式输入日期。<br /><span style="color:#f00">必填项目</span>');
vform.rules.add('qq',0,'number','这必须是一个整数');
vform.rules.add('least10',10,'any','您必须至少填写10个<br /><span style="color:#f00">必填项目</span>');
vform.rules.add('ok100',1,'any','这里被限制为100个字符<br /><span style="color:#f00">必填项目</span>',100);
vform.init();
}
<body onload="start();">

发布了90 篇原创文章 · 获赞 40 · 访问量 10万+

Guess you like

Origin blog.csdn.net/webxuexi168/article/details/104433728
Recommended