Home > Web Front-end > JS Tutorial > Summary of common javascript functions_javascript skills

Summary of common javascript functions_javascript skills

WBOY
Release: 2016-05-16 15:55:36
Original
1210 people have browsed it

1. input can only input integers, 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,''));  // 只能输入整数数字和字母
});

Copy after login

Many other implementation methods using Baidu have problems. They cannot accurately represent "only numbers and letters can be entered" because they pre-enter punctuation marks, such as allowing decimal points, . and other symbols to be entered. For example, the following answer from Baidu:

value=value.replace(/[^\w\.\/]/ig,'')
value=value.replace(/[^\d|chun]/g,'')
value=value.replace(/[^\w\.\/]/ig,'')
Copy after login

The above 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不能为空");
    $(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格式错误");
      $(myThis).focus();
      return;
    }
  }
  $("#email_error").text("");
}

Copy after login

3. Extract integers and English letters from strings

$(function(){
  var a = 'testAbc,。、,./电饭锅123def'; 
  b = a.replace(/[^0-9]+/ig,"");
  alert(b);
  b = a.replace(/[^a-z]+/ig,"");
  alert(b);
});
Copy after login

4. Use of jquery cookie plug-in

  var isFs = $(this).attr("datas");
  $.cookie("isFs",isFs,{ expires: 7 });
Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template