jQuery jquery中的邮政编码验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11472694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:38:45  来源:igfitidea点击:

zip code validation in jquery

jqueryvalidation

提问by user1334095

$('#create_membership').click(function () {
    var zip = $('#zip').val();

    else if (zip == ''){
        errorMessage = "*Zipcode required!";
    }
    else if ((zip.length)< 5 || (zip.length)>5 ){
        errorMessage = "*zipcode should only be 5 digits";
    }
    else if ( zip =( "^[0-9]+$" )){
        errorMessage = "*zipcode should be numbers only";
    }

I am getting first and second i.e., empty and length validations for zip code.but for the 3rd case,number validation is not working.can any one help me for getting the number validation thanks in advance

我得到了第一个和第二个,即邮政编码的空和长度验证。但是对于第 3 种情况,数字验证不起作用。任何人都可以帮助我获得数字验证,提前致谢

回答by Jimmy Sawczuk

This isn't really jQuery-specific, but you have several syntax errors and your validation logic can be greatly simplified:

这并不是真正特定于 jQuery,但您有几个语法错误,您的验证逻辑可以大大简化:

$('#create_membership').click(function()
{
    var zip = $('#zip').val();

    var zipRegex = /^\d{5}$/;

    if (!zipRegex.test(zip))
    {
        // trigger error
    }
    else
    {
        // success!
    }
});

I should note that validating a ZIP code isn't necessarily just checking for 5 numbers. I'd also try and find a listing somewhere and make sure what they entered is a valid US ZIP code. Additionally, you might also want to allow for the 4-digit supplemental code as well.

我应该注意,验证邮政编码不一定只是检查 5 个数字。我还会尝试在某处找到清单,并确保他们输入的是有效的美国邮政编码。此外,您可能还希望允许使用 4 位补充代码。

回答by Developer

You need to treat the regular expression like a function, otherwise you're just assigning the value to "zip". See below:

您需要将正则表达式视为函数,否则您只是将值分配给“zip”。见下文:

$('#create_membership').click(function () {
  var zip = $('#zip').val();
  var reg = /^[0-9]+$/;
  else if (zip == ''){
  errorMessage = "*Zipcode required!";
  }
  else if ((zip.length)< 5 || (zip.length)>5 ){
  errorMessage = "*zipcode should only be 5 digits";
  }
  else if (!reg.test(zip)){
  errorMessage = "*zipcode should be numbers only";
  }
});

回答by Pirozek

You can try something like this - How to allow only numeric (0-9) in HTML inputbox using jQuery?

您可以尝试这样的操作 -如何使用 jQuery 在 HTML 输入框中只允许数字 (0-9)?

If you want just the regexp, try this - jquery: validate that text field is numeric

如果你只想要正则表达式,试试这个 - jquery:validate that text field is numeric

回答by swaroop suthar

number validation

号码验证

var data=$('#zip').val();
var len=data.length;
var c=0;
for(var i=0;i<len;i++)
{
  c=data.charAt(i).charCodeAt(0);
  if(c <48 || c >57)
  {
    $('.error').show();
    event.preventDefault();
    break;
  }
  else
  {
    $('.error').hide();
  }
}