-1

I have a requirement where input string should not be an empty string (all characters allowed) and length should be in between 1 to 500.

I found working expressions for separately, one for checking empty string and one for length. I'm unable to club them to result an expression which should tell me if input is 'Empty string' or more than 500 chars.

jsfiddle here

for empty string

var regex = /^\s*$/;

for length

{1,499}
Mr Mystery Guest
  • 1,464
  • 1
  • 18
  • 47
user2555212
  • 165
  • 1
  • 14
  • 2
    *"jsfiddle here"* The full content of your question must be **in** your question, not just linked. Links rot, making the question and its answers useless to people in the future, and people shouldn't have to go off-site to help you. Put a [mcve] **in** the question, ideally using Stack Snippets (the `<>` toolbar button) to make it runnable. More: [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Aug 29 '17 at 09:32
  • 2
    Shouldn't be an empty string and length is between 0 and 500? there is a contradiction there. Perhaps you meant [1 , 499] – DjaouadNM Aug 29 '17 at 09:34
  • yes Mr geek. updated the description – user2555212 Aug 29 '17 at 09:37
  • Is a 500-character string allowed or not? – T.J. Crowder Aug 29 '17 at 09:39
  • 500 character string is allowed. if string is more than 500 characters its not allowed – user2555212 Aug 29 '17 at 09:40

3 Answers3

3

From your example checking for an "empty" string, it's clear you want to ignore whitespace at the beginning or end.

The simplest way to do this, if you don't actually have a regex requirement, is just trim and check the length:

str = str.trim();
if (str.length && str.length <= 500) {
    // All good
}

But if you need a regex (perhaps an HTML5 validation regex), then it's simply a matter of saying 1. "ignore whitespace at the beginning, 2. require 1-500 characters in the middle, and 3. ignore whitespace at the end." (with anchors to ensure we're matching the full string):

//          111          333
// ---------vvv----------vvv
var rex = /^\s*\S.{0,499}\s*$/;
// ------------^^^^^^^^^^
//             2222222222

Note the \S which requires at least one non-whitespace character there, followed by 0 to 499 other characters.

Note: You'll need to trim the string at some point, though, since the whitespace at one end of the other can take you over the 500 limit.

Example usage:

// NOTE: Using 5 instead of 500 to make tests shorter
var rex = /^\s*\S.{0,4}\s*$/;
console.log(rex.test("")); // false
console.log(rex.test("a")); // true
console.log(rex.test(" a ")); // true
console.log(rex.test(" 12345 ")); // true
console.log(rex.test(" 123456 ")); // false
console.log(rex.test("123456")); // false
console.log(rex.test("1234       ")); // true
console.log(rex.test("       ")); // false
.as-console-wrapper {
  max-height: 100% !important;
}

Note: In JavaScript, . doesn't match line termination characters. If you want to allow those, use [\w\W] (anything that's a word character or not a word character) instead of .: /^\s*[\w\W]{1,500}\s*$/

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @user2555212 However, this will match strings that have more than 500 chars. Your requirement is *length should be in between 1 to 500*. – Wiktor Stribiżew Aug 29 '17 at 10:11
  • @WiktorStribiżew: I interpreted it as "1-500 characters ignoring whitespace at either end" (as I said at the beginning). – T.J. Crowder Aug 29 '17 at 10:18
  • @T.J.Crowder The first paragraph is quite clear: *input string should not be an empty string (all characters allowed) and length should be in between 1 to 500*. Your solution is not meeting the second requirement. Otherwise, the question is not clear. – Wiktor Stribiżew Aug 29 '17 at 10:19
  • @WiktorStribiżew: See the regex for "empty string" (which, again, I called out in my first sentence). – T.J. Crowder Aug 29 '17 at 10:26
  • @user2555212: To be clear: The string could have more than 500 chars if there's whitespace on either end (in fact, one of the tests above shows that); you'd presumably trim if you want to remove those. – T.J. Crowder Aug 29 '17 at 10:27
  • @T.J.Crowder Even if you are right about the number of chars, and there can be more than 500 all in all, the `^\s*.{1,500}\s*$` regex can match a blank string that OP seems to avoid matching. – Wiktor Stribiżew Aug 29 '17 at 10:51
  • @WiktorStribiżew: Very good point, I kinda missed that out in my list of tests, didn't I? Thanks, fixed. – T.J. Crowder Aug 29 '17 at 11:03
  • @user2555212: Note fix above. – T.J. Crowder Aug 29 '17 at 11:03
0

Regular expressions are a bit over the top for your needs. What's wrong with...

if (myString == null || myString.length > 500){
    // handle failed validation here
}

EDIT

Above will allow empty string to pass. For between 1 and 500 use

if (myString == null || myString.length < 1 || myString.length > 500){
    // handle failed validation here
}
phuzi
  • 12,078
  • 3
  • 26
  • 50
0

Use

/^(?!\s+$)[\s\S]{1,500}$/

It matches

  • ^ - start of string
  • (?!\s+$) - no 1 or more whitespaces up to the end of string (a string that consists of 1 or more whitespaces is failed here)
  • [\s\S]{1,500} - any 1 to 500 chars (so, no empty string is allowed)
  • $ - end of string.

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563