JavaScript common features Summary _javascript tips

Source: Internet
Author: User

1. Input can only enter integer numbers and letters

$ (document). On (' KeyUp ', ' #no ', function () {
  var val = $.trim ($ (this). Val ());
  if (val = = NULL | | val = = ") return
    ;
  
  $ (this). Val (Val.replace (/[^0-9a-z]+/ig, '));  Can only enter integer numbers and Letters
});

Many other implementations that come out of Baidu are problematic. They do not accurately represent "only numbers and letters" because they enter punctuation in advance, such as allowing a decimal point to be entered. and other symbols. For example, the following is a Baidu out of the answer:

Value=value.replace (/[^\w\.\/]/ig, ')
value=value.replace (/[^\d|chun]/g, ')
Value=value.replace (/[^\w \.\/]/ig, "")

The answers are all problematic.

2. Email format Verification

function Validate_email (mythis) {
  var val = $.trim ($ (mythis). Val ());
  if (val = = NULL | | val = = "") {
    $ ("#email_error"). Text ("Email cannot be empty");
    $ (mythis). focus ();
    return;
  }
    
  if (Val!= null && val!= "") {
    if!/^[a-za-z0-9_-]+@[a-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) +$/g.test (val)) {
      $ ("#email_error"). Text ("Email format error");
      $ (mythis). focus ();
      return;
    }
  $ ("#email_error"). Text ("");


3. Extract integer numbers and English letters from a string

$ (function () {
  var a = ' testabc,. 、,./Rice Cooker 123def '; 
  b = A.replace (/[^0-9]+/ig, "");
  alert (b);
  b = A.replace (/[^a-z]+/ig, "");
  alert (b);
});

4. jquery Cookie Plugin Usage

  var Isfs = $ (this). attr ("Datas");
  $.cookie ("Isfs", isfs,{expires:7});

The above mentioned is the entire content of this article, I hope you can enjoy.

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.