admin管理员组文章数量:1026989
I want to do a client side validation for an input field called 'City or Postal Code', I have separate validations for the user input. I first differentiate if the input is a number or string, and then have separate blocks to validate them. I would like to know if there is an elegant way or in other words, a single regex to validate the input
validateCityOrPostalCode(value) {
const isnum = /^\d+$/.test(value);
if (isnum) {
const patternForZipCode = RegExp('^[0-9]{5}$');
const result = patternForZipCode.test(value);
return result;
}
const onlyLetters = RegExp('^[a-zA-Z ]$').test(value);
if (onlyLetters) {
const patternForCity = RegExp('^([a-zA-Z -]{1,50})$');
const validCityName = patternForCity.test(value);
return validCityName;
}
}
What I intend on doing
validateCityOrPostalCode(value) {
const patternForCityOrPostalCode = '<The regex that I am looking for >'
const result = patternForCityOrPostalCode.test(value);
return result;
}
Valid cases: 5 digit zip codes like 10016, 12345, 44444 etc (5 digits exactly) Cities like 'New York', 'Boston', 'Chicago', xx, xxxx etc (1 to 50 characters)
Invalid cases: 1234 123 12 1 empty string Boston33 333Chicago
I want to do a client side validation for an input field called 'City or Postal Code', I have separate validations for the user input. I first differentiate if the input is a number or string, and then have separate blocks to validate them. I would like to know if there is an elegant way or in other words, a single regex to validate the input
validateCityOrPostalCode(value) {
const isnum = /^\d+$/.test(value);
if (isnum) {
const patternForZipCode = RegExp('^[0-9]{5}$');
const result = patternForZipCode.test(value);
return result;
}
const onlyLetters = RegExp('^[a-zA-Z ]$').test(value);
if (onlyLetters) {
const patternForCity = RegExp('^([a-zA-Z -]{1,50})$');
const validCityName = patternForCity.test(value);
return validCityName;
}
}
What I intend on doing
validateCityOrPostalCode(value) {
const patternForCityOrPostalCode = '<The regex that I am looking for >'
const result = patternForCityOrPostalCode.test(value);
return result;
}
Valid cases: 5 digit zip codes like 10016, 12345, 44444 etc (5 digits exactly) Cities like 'New York', 'Boston', 'Chicago', xx, xxxx etc (1 to 50 characters)
Invalid cases: 1234 123 12 1 empty string Boston33 333Chicago
Share Improve this question asked Aug 8, 2017 at 14:31 Brr SwitchBrr Switch 9841 gold badge9 silver badges22 bronze badges1 Answer
Reset to default 3You can use a pipe in your regex, which acts like an OR.
Example snippet:
function validateCityOrPostalCode(value) {
return /^([0-9]{5}|[a-zA-Z][a-zA-Z ]{0,49})$/.test(value);
}
var values = ['12345','New York','1234','',' ','Boston33','333Chicago'];
values.forEach(
function(value) {
console.log(value +":"+ validateCityOrPostalCode(value));
});
I want to do a client side validation for an input field called 'City or Postal Code', I have separate validations for the user input. I first differentiate if the input is a number or string, and then have separate blocks to validate them. I would like to know if there is an elegant way or in other words, a single regex to validate the input
validateCityOrPostalCode(value) {
const isnum = /^\d+$/.test(value);
if (isnum) {
const patternForZipCode = RegExp('^[0-9]{5}$');
const result = patternForZipCode.test(value);
return result;
}
const onlyLetters = RegExp('^[a-zA-Z ]$').test(value);
if (onlyLetters) {
const patternForCity = RegExp('^([a-zA-Z -]{1,50})$');
const validCityName = patternForCity.test(value);
return validCityName;
}
}
What I intend on doing
validateCityOrPostalCode(value) {
const patternForCityOrPostalCode = '<The regex that I am looking for >'
const result = patternForCityOrPostalCode.test(value);
return result;
}
Valid cases: 5 digit zip codes like 10016, 12345, 44444 etc (5 digits exactly) Cities like 'New York', 'Boston', 'Chicago', xx, xxxx etc (1 to 50 characters)
Invalid cases: 1234 123 12 1 empty string Boston33 333Chicago
I want to do a client side validation for an input field called 'City or Postal Code', I have separate validations for the user input. I first differentiate if the input is a number or string, and then have separate blocks to validate them. I would like to know if there is an elegant way or in other words, a single regex to validate the input
validateCityOrPostalCode(value) {
const isnum = /^\d+$/.test(value);
if (isnum) {
const patternForZipCode = RegExp('^[0-9]{5}$');
const result = patternForZipCode.test(value);
return result;
}
const onlyLetters = RegExp('^[a-zA-Z ]$').test(value);
if (onlyLetters) {
const patternForCity = RegExp('^([a-zA-Z -]{1,50})$');
const validCityName = patternForCity.test(value);
return validCityName;
}
}
What I intend on doing
validateCityOrPostalCode(value) {
const patternForCityOrPostalCode = '<The regex that I am looking for >'
const result = patternForCityOrPostalCode.test(value);
return result;
}
Valid cases: 5 digit zip codes like 10016, 12345, 44444 etc (5 digits exactly) Cities like 'New York', 'Boston', 'Chicago', xx, xxxx etc (1 to 50 characters)
Invalid cases: 1234 123 12 1 empty string Boston33 333Chicago
Share Improve this question asked Aug 8, 2017 at 14:31 Brr SwitchBrr Switch 9841 gold badge9 silver badges22 bronze badges1 Answer
Reset to default 3You can use a pipe in your regex, which acts like an OR.
Example snippet:
function validateCityOrPostalCode(value) {
return /^([0-9]{5}|[a-zA-Z][a-zA-Z ]{0,49})$/.test(value);
}
var values = ['12345','New York','1234','',' ','Boston33','333Chicago'];
values.forEach(
function(value) {
console.log(value +":"+ validateCityOrPostalCode(value));
});
本文标签: JavaScript regex to validate postal code or CityStack Overflow
版权声明:本文标题:JavaScript regex to validate postal code or City - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745655537a2161577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论