Common Javascript methods used in HTML to verify form text fields, such as dates, strings, and numbers

Source: Internet
Author: User
Tags rtrim

// ============== Verification function start <br/> var isie = false; <br/> var isff = false; <br/> var Issa = false; <br/> If (navigator. useragent. indexof ("MSIE")> 0) & (parseint (navigator. appversion) >=4) {<br/> isie = true; <br/>}< br/> If (navigator. useragent. indexof ("Firefox")> 0) {<br/> isff = true; <br/>}< br/> If (navigator. useragent. indexof ("safari")> 0) {<br/> Issa = true; <br/>}</P> <p> // function: control Function input number <br/> func Tion onlynumber (e) {<br/> var key; <br/> ikeycode = Window. event? E. keycode: E. Which; <br/> If (! (Ikeycode> = 48) & (ikeycode <= 57) | (ikeycode = 13) | (ikeycode = 46) | (ikeycode = 45) | (ikeycode = 37) | (ikeycode = 39) | (ikeycode = 8 ))) {<br/> If (isie) {<br/> E. returnvalue = false; <br/>}else {<br/> E. preventdefault (); <br/>}</P> <p> // text box: <input type = "text" maxlength = "8" class = "input" name = "txtname" onkeypress = "Return onlynumber (event)" style = "ime-mode: dis Abled "size =" 25 "/> <br/>/* <br/> prohibit the submission of non-decimal or non-integer data. Three steps are required: <br/> 1. text Box event onkeypress = "Return onlynumber (event)" <br/> 2. text Box style = "ime-mode: Disabled" <br/> 3. add <br/> If (validatenumber ('txtname') {<br/> document. form1.submit (); <br/>}< br/> */<br/> // if a non-number is included, false is returned. Allowed decimal point <br/> function validatenumber () {<br/> var Len = arguments. length; <br/> var flag = true; <br/> for (VAR I = 0; I <arguments. length; I ++) {<br/> flag = numericcheck (arguments [I]); <br/>}< br/> return flag; <br/>}</P> <p> // function: only numbers can be entered, and a prompt is displayed for several unacceptable characters. <br/> function numericcheck (parameter) {<br/> var El = document. getelementbyid (parameter); <br/> NR1 = document. getelementbyid (parameter ). value; <br/> Flg = 0; <br/> STR = ""; <br/> SPC = ""; <br/> arw = ""; <br/> for (VAR I = 0; I <nr1.length; I ++) {<br/> CMP = ". 0123456789 "; <br/> TST = nr1.substring (I, I + 1); <br/> If (CMP. indexof (TST) <0) {<br/> flg ++; <br/> STR + = "" + TST; <br/> SPC + = TST; <br/> arw + = "^"; <br/>}else {<br/> arw + = "_"; <br/>}< br/> If (flg! = 0) {<br/> If (SPC. indexof ("")>-1) {<br/> STR + = "and space"; <br/>}< br/> var prevnode = el. parentnode. previussibling. innertext; <br/> alert (prevnode + "only numbers are allowed, and" + flg + "unacceptable characters/N" are found); <br/> el. select (); <br/> el. focus (); <br/> return false; <br/>} else {<br/> return true; <br/>}</P> <p> // determines whether the date is valid and can effectively verify the leap year. Supported date formats include: 2009-01-01, 2009/01/01 <br/> // parameter: datevalue <br/> function isdate (datevalue) {<B R/> var RegEx = new Regexp ("^ (? :(? :( [0-9] {4} (-| /)(? :(?: 0? [1, 3-9] | 1 [0-2]) (-| /)(?: 29 | 30) | ((?: 0? [13578] | 1 [02]) (-|/) 31) | ([0-9] {4} (-| /)(?: 0? [1-9] | 1 [0-2]) (-| /)(?: 0? [1-9] | 1 // d | 2 [0-8]) | (((? :( // D (?: 0 [48] | [2468] [048] | [13579] [26]) | (?: 0 [48] 00 | [2468] [048] 00 | [13579] [26] 00) (-|/) 0? 2 (-|/) 29) $ "); <br/> If (! RegEx. test (datevalue) {<br/> return false; <br/>} else {<br/> return true; <br/>}</P> <p> // comparison date. Currently, only the year-month-day format is supported. If senddate is later than sbegindate, returns true, <br/> // parameter: sbegindate <br/> // senddate <br/> function comparedate (sbegindate, senddate) {<br/> var strdt1 = sbegindate. replace ("-", "/"); <br/> var strdt2 = senddate. replace ("-", "/"); <br/> var dt1 = new date (date. parse (strdt1); <br/> var dt2 = new dat E (date. parse (strdt2); <br/> If (dt1 <= dt2) {<br/> return true; <br/>} else {<br/> return false; <br/>}</P> <p> // ============= verify the function end </P> <p> // string operation function <br/> string. prototype. trim = function () {<br/> return this. replace (/(^/S *) | (/S * $)/g, ""); <br/>}; <br/> string. prototype. ltrim = function () {<br/> return this. replace (/(^/S *)/g, ""); <br/>}; <br/> string. prototype. rtrim = function () {<br/> retu Rn this. replace (/S * $)/g, ""); <br/>}; </P> <p> // function: delete spaces between the left and right sides <br/> // parameter: Str, string to be processed <br/> function trim (STR) {<br/> return Str. replace (/(^/S *) | (/S * $)/g, ""); <br/>}</P> <p> // role: delete the left space <br/> // parameter: Str, string to be processed <br/> function ltrim (STR) {<br/> return Str. replace (/(^/S *)/g, ""); <br/>}</P> <p> // function: delete spaces on the right <br/> // parameter: Str, string to be processed <br/> function rtrim (STR) {<br/> return Str. replace (/S * $)/g, ""); <br/>}</P> <p>/ /Purpose: remove the Left and Right spaces and determine whether the length of the value is 0. If it is true, false if it is greater than 0 <br/> // parameter: value <br/> function ifvaluelengthiszero (value) {<br/> var v = trim (value); <br/> If (v. length <1) {<br/> return true; <br/>}< br/> return false; <br/>}</P> <p> // function: string operation function, digital verification <br/> // example: <input type = "text" name = "code" size = "10" maxlength = "6" onkeypress = "javascript: numinputonly ();" <br/> function numinputonly () {<br/> If (event. keycode> 57 | event. Keycode <48) {<br/> event. keycode = 0; <br/>}</P> <p> // determines whether it is an input integer. <br/> // parameter: S, input string <br/> function isint (s) {<br/> If (S. value = "") {<br/> return true; <br/>}< br/> var chk = parseint (S. value, 10); <br/> If (chk! = S. Value | chk <0) {<br/> alert ("this field should be a positive integer! "); <Br/> S. focus (); <br/> S. select (); <br/> return false; <br/>}< br/> return true; <br/>}</P> <p> // function: floating Point number input judgment <br/> // parameter: OBJ input parameter <br/> function isfloat (OBJ) {<br/> If (obj. value = "") {<br/> return true; <br/>}< br/> var chk = parsefloat (obj. value); <br/> If (chk! = Obj. Value) {<br/> alert ("this field should be an integer or decimal number! "); <Br/> obj. focus (); <br/> obj. select (); <br/> return false; <br/>}< br/> return true; <br/>}</P> <p> // function: obtain the string length <br/> // parameter: value, input string <br/> function strlen (value) {<br/> var STR, num = 0; <br/> for (VAR I = 0; I <value. length; I ++) {<br/> STR = value. substring (I, I + 1); <br/> If (STR <= "~ ") {<Br/> num + = 1; <br/>}else {<br/> num + = 2; <br/>}< br/> return num; <br/>}</P> <p> // minimum length (cannot be blank) <br/> // parameter: value, input string <br/> function vminlength (OBJ, minlength) {<br/> If (TRIM (obj. value ). length <minlength) {<br/> obj. focus (); <br/> obj. select (); <br/> return false; <br/>}< br/> return true; <br/>}</P> <p> // function: maximum length (cannot be blank) <br/> // parameter: value, input string <br/> function vmaxlength (OBJ, maxlength) {<br /> If (TRIM (obj. value ). length> maxlength) {<br/> obj. focus (); <br/> obj. select (); <br/> return false; <br/>}< br/> return true; <br/>}</P> <p> // function: judge whether the input exceeds the character length set by the database <br/> // parameter: Str, field on the page <br/> // validstrlength, database length <br/> // strname, indicating the field name <br/> // call guide: <br/>/* If (! Truelength (vcjlzdbdh. value, 50, "certificate number") {<br/> vcjlzdbdh. focus (); <br/> return (false); <br/>}< br/> If the length is exceeded, "the length of the certificate number you entered exceeds the maximum allowed value." <Br/> */<br/> function truelength (STR, validstrlength, strname) {<br/> var R; <br/> var I; <br/> var COUNT = 0; <br/> var ret; <br/> for (I = 0; I <Str. length; I ++) {<br/> r = Str. charcodeat (I, 1); <br/> If (r> 255) {<br/> COUNT = count + 2; <br/>}else {<br/> COUNT = count + 1; <br/>}< br/> If (count> validstrlength) {<br/> alert ("the length of your input" + strname + "exceeds the maximum allowed value. "); <Br/> ret = false; <br/>}else {<br/> ret = true; <br/>}< br/> return ret; <br/>}</P> <p>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.