最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

JavaScript regex to validate postal code or City - Stack Overflow

matteradmin8PV0评论

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));
});

Post a comment

comment list (0)

  1. No comments so far