0

我有一个 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;
}
4

3 回答 3

5

验证街道地址至少 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;
  }
}
于 2013-05-08T08:21:29.447 回答
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;
}
于 2013-05-08T08:25:18.547 回答
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;
}
于 2013-05-08T08:23:54.643 回答