admin管理员组

文章数量:1435859

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 badges
Add a ment  | 

1 Answer 1

Reset to default 3

You 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