Some verified JS files in the company project are shown to others. I will copy them to my blog. It will be more convenient to copy them in the future ......
// Test whether the value of the input box is an integer <br/> function checkNum (inputObj) {<br/> var regex =/^/d + $/; <br/> return regex. test (inputObj. value); <br/>}< br/> // number of values in the test input box <br/> function checkLength (inputObj, maxLength) {<br/> return inputObj. value. length <= maxLength; <br/>}< br/> // remove all spaces at the beginning and end of the string (after the js is referenced on the page. trim (), that is, spaces with the modified values can be removed. <br/> String. prototype. trim = function () {<br/> return this. replace (/(^/s *) | (/s * $)/g, ""); <br/ >}< Br/> // test whether the input box is null <br/> function checkInputValueIsEmpty (inputObj) {<br/> var s = inputObj. value. trim (); <br/> if (s = null | s = "") {<br/> return false; <br/>}< br/> else {<br/> return true; <br/>}< br/> // verify the decimal place specified by the maximum length. inputObj is the input object, and beforeLength is the number of digits before the decimal point, afterLength is the number of digits after the decimal point <br/> function checkNumberLength (inputObj, beforeLength, afterLength) {<br/> if (inputObj. value. index Of (".") >=0) {<br/> var regex = new RegExp ("^ // d {0," + beforeLength + "} [.]? // D {0, "+ afterLength +"} $ "); <br/> return regex. test (inputObj. value); <br/>}< br/> else {<br/> var regex = new RegExp ("^ // d {0, "+ beforeLength +" }$ "); <br/> return regex. test (inputObj. value); <br/>}< br/> // verify the positive and negative decimals specified by the maximum length. inputObj is the input object, and beforeLength is the number of digits before the decimal point, afterLength is the number of digits after the decimal point <br/> function checkAllNumberLength (inputObj, beforeLength, afterLength) {<br/> if (inputObj. value. index Of (".") >=0) {<br/> var regex = new RegExp ("^ -? // D {0, "+ beforeLength +"} [.]? // D {0, "+ afterLength +"} $ "); <br/> return regex. test (inputObj. value); <br/>}< br/> else {<br/> var regex = new RegExp ("^ -? // D {0, "+ beforeLength +"} $ "); <br/> return regex. test (inputObj. value); <br/>}< br/> // test whether the value of the input box contains decimals. <br/> function checkNumber (inputObj) {<br/> var regex =/^ ([0-9] *) ([.]?) ([0-9] *) $/; <br/> return regex. test (inputObj. value); <br/>}< br/> // test whether the value of the input box is decimal. <br/> function checkAllNumber (inputObj) {<br/> var regex =/^ -? ([0-9] *) ([.]?) ([0-9] *) $/; <br/> return regex. test (inputObj. value); <br/>}< br/> // Test ID card verification <br/> function checkIDnumber (inputObj) {<br/> var regex =/(^/d {15} $) | (^/d {17} ([0-9] | X) $ )/; <br/> return regex. test (inputObj. value); <br/>}< br/> // Test ID card verification <br/> function checkemail (inputObj) {<br/> var regex =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) +/; <br/> return regex. test (inputObj. value); <br/>}< br/> // enter the maximum length. InputObj is the input object, beforeLength is the number of digits before the decimal point, and afterLength is the number of digits after the decimal point <br/> function KeyPress (inputObj, beforeLength, afterLength) {// only data characters 0-9 and decimal point can be entered <br/> var keyvalue = String. fromCharCode (event. keyCode); // obtain the keyValue Based on the keyCode <br/> var inputValue = inputObj. value + keyvalue; <br/> var testValue = false; <br/> if (inputValue. indexOf (". ")> = 0) {<br/> var regex = new RegExp (" ^ // d {0, "+ beforeLe Ngth + "} [.]? // D {0, "+ afterLength +"} $ "); <br/> testValue = regex. test (inputValue); <br/>}< br/> else {<br/> var regex = new RegExp ("^ // d {0, "+ beforeLength +" }$ "); <br/> testValue = regex. test (inputValue); <br/>}< br/> if (! TestValue) {<br/> event. returnValue = false; <br/>}< br/> // enter the maximum length of positive and negative decimals. inputObj is the input object, and beforeLength is the number of digits before the decimal point, afterLength is the number of digits after the decimal point <br/> function KeyPressAllNumber (inputObj, beforeLength, afterLength) {// only all decimals can be input <br/> var keyvalue = String. fromCharCode (event. keyCode); // obtain the keyValue Based on the keyCode <br/> var inputValue = inputObj. value + keyvalue; <br/> var testValue = false; <br/> if (inputVa Lue. indexOf (".") >=0) {<br/> var regex = new RegExp ("^ -? // D {0, "+ beforeLength +"} [.]? // D {0, "+ afterLength +"} $ "); <br/> testValue = regex. test (inputValue); <br/>}< br/> else {<br/> var regex = new RegExp ("^ -? // D {0, "+ beforeLength +"} $ "); <br/> testValue = regex. test (inputValue); <br/>}< br/> if (! TestValue) {<br/> event. returnValue = false; <br/>}< br/> // enter an integer <br/> function KeyPressNum (objTR) {// only data characters 0-9 can be entered <br/> // var objTR = element.doc ument. activeElement; <br/> var txtval = objTR. value; <br/> var key = event. keyCode; <br/> if (key <48 | key> 57) {<br/> event. keyCode = 0; <br/>}< br/> function checkInputValueIsEmptyOrOverLength (inputObj, inputLength, inputField) {<br /> If (! CheckInputValueIsEmpty (inputObj) {<br/> alert (inputField + "cannot be blank! "); <Br/> return false; <br/>}< br/> if (! CheckLength (inputObj, inputLength) {<br/> alert (inputField + "the input value exceeds the maximum length! "); <Br/> return false; <br/>}< br/> return true; <br/>}< br/> // convert the text of inputObj to a halfwidth. <br/> function DBC2SBC (inputObj) {<br/> // If the left and right direction keys are used, directly return <br/> if (event. keyCode = 37 | event. keyCode = 39) {<br/> return; <br/>}< br/> var result = ""; <br/> for (var I = 0; I <inputObj. value. length; I ++) {<br/> code = inputObj. value. charCodeAt (I); // obtain the unicode code of the current character <br/> if (code >=65281 & code <= 65373 )// In this unicode encoding range, all English letters and characters <br/>{< br/> result + = String. fromCharCode (inputObj. value. charCodeAt (I)-65248 ); <br/> // convert the unicode code of the full-width character to the unicode code of the corresponding half-width character <br/>}< br/> else if (code = 12288) // space <br/>{< br/> result + = String. fromCharCode (inputObj. value. charCodeAt (I)-12288 + 32); <br/>}< br/> else {<br/> result + = inputObj. value. charAt (I); <br/>}< br/> inputObj. value = result; <br/ >}< Br/> example of adding a button: <br/> <asp: TextBox ID = "txtBDDW" runat = "server" onkeypress = "KeyPress (this, 4, 2); "CssClass =" input01 "<br/> onfocus =" this. className = 'input02' "onblur =" this. className = 'input01' "onkeyup =" DBC2SBC (this); "> </asp: textBox> <span> <font <br/> color = "red"> * </font> </span> only four integers with two decimal places are allowed. <br/> js verification example: <br/> -- non-empty verification <br/> if (document. getElementById ('txtcwenkongzai '). value. trim () = "") {<br/> aler T ("enter normal temperature and no load! "); <Br/> document. getElementById ('txtcwenkongzai '). focus (); </p> <p> return false; <br/>}< br/> -- prevents numeric input, by copying the length that exceeds the limit of the text box <br/> if (checkNumberLength (document. getElementById ('txtcwenqinzai'), 6, 2) = false) {<br/> alert ("Normal Temperature light loading format error! ") <Br/> document. getElementById ('txtcwenqinzai'). focus (); <br/> return false; <br/>}