使用jQuery检查街道地址的最小字符

时间:2013-05-08 08:17:13

标签: jquery

我有一个jQuery函数来检查邮政编码中的数字字符。我还需要检查街道地址的最小字符数(10)。

function paymentStep1(){

jQuerychk = jQuery.noConflict();
var numbers = /^[0-9]+$/; 
var zip = jQuerychk("input#id_billing_detail_postcode").val();  
if (zip.match(numbers))  {
document.getElementById("errormsssgen").innerHTML = '';
}else{      
    document.getElementById("errormsssgen").innerHTML = "ZIP code must have numeric characters only."
    return false;
}

3 个答案:

答案 0 :(得分:4)

验证街道地址至少10个字符:

function validateStreetAddress(value){
  var l = value.trim().length;
  if (l < 10) {
    alert("Error: Street Address must be a minimum of 10 characters!");
    return false;
  }
}

答案 1 :(得分:1)

除了Zee Tee的回答,您不需要正则表达式来验证邮政编码,如果您不接受空格。您可以改为使用isNaN()功能。

if (!isNaN(numbers)) {
    document.getElementById("errormsssgen").innerHTML = '';
}else{      
    document.getElementById("errormsssgen").innerHTML = "ZIP code must have numeric characters only."
    return false;
}

答案 2 :(得分:1)

你可以这样做:

// Get the street address from the textbox first
var street_address = jQuerychk("#street_address").val();

// Now trim it for the extra spaces
street_address = jQuerychk.trim(street_address);

// Get the length of data entered as street address 
var n = street_address.length;

// Compare and get the appropiate meesage
if (n > 10) {
    jQuerychk("#errormsssgen").html('');
} else {
    jQuerychk("#errormsssgen").html('Street adress must be min length 10 chars');
    return false;
}