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

Summary of common javascript functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:55:361176browse

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,''));  // 只能输入整数数字和字母
});

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,'')

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("");
}

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);
});

4. Use of jquery cookie plug-in

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

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

Statement:
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