Common JS function collection (remove spaces, verify email addresses, dates, positive integers, etc)

Source: Internet
Author: User
Tags smarty template

Some functions commonly used in Javascript are collected and shared. The specific functions are as follows: functions that return the id attribute value node from the original ecology, remove spaces on the left of the string, remove spaces on the right of the string, and remove spaces on the string twice, verify email address, verify the number range of strings, verify the date format, verify whether a positive integer, and so on.

// ****************** Return the node value of the id attribute *************** * ********** function $ (id) {return document. getElementById (id );} // ****************** remove the left space ***************** * ******* function ltrim (s) {return s. replace (/^ \ s */,"");} // ****************** remove the right space ***************** * ******* function rtrim (s) {return s. replace (/\ s * $ /,"");} // ******************* remove left and right spaces ***************** * ******* function trim (s) {return ltrim (rtrim (s ));}// ***************** * ******* Function checkcharnum_range (str, lt, gt) {if (trim ($ (str ). value ). length <lt) {return false;} if (trim ($ (str ). value ). length> gt) {return false ;}} ****************** * ****** function isEmail (str) {var reg =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((\. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $/; return reg. test (str );} // ****************** verify the integer ************** * *********** Function isInteger (str) {var regu =/^ [-] {0, 1} [0-9] {1 ,} $/; return regu. test (str );} ****************** * ****** function isDate (str) {if (trim (str) = "") return false; var r = str. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d {}) $ /); // year, month, and day Regular Expression if (r = null) {alert ("enter the correct date \ n \ r date format: yyyy-mm-dd \ n \ r example: 2008-08-08 \ n \ r "); return false;} var d = new Date (r [1], r [3]-1, r [4]); var num = (d. ge TFullYear () = r [1] & (d. getMonth () + 1) = r [3] & d. getDate () = r [4]); if (num = 0) {alert ("enter the correct date \ n \ r date format: yyyy-mm-dd \ n \ r example: 2008-08-08 \ n \ r ");} return (num! = 0 );} // ***************** verify the positive integer ****************** * ****** function isNum (str) {var reg =/^ [1-9] \ d * $/; return reg. test (str );}
Articles you may be interested in
  • Collect common php functions with explanations
  • Php uses the filter function to verify the mailbox, url, and IP address
  • PHP compresses html webpage code to reduce the amount of network data transmitted, clear spaces, tabs, and comment mark
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • How does php clear html format, remove spaces in text, and intercept text?
  • Php uses array_flip to implement array key-value exchange to remove array duplicate values
  • Php uses the ZipArchive function to compress and decompress files.
  • Php extracts the birthday date from the ID card number and verifies whether it is a minor.

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.