Page 1 of 1

Check Length Of String Using Regular Expression (RegEx) Rate Topic: -----

#1 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Posted 07 May 2008 - 04:34 PM

Background Information
Often times when creating a webpage I'll need to do some validation on the fields. I don't always want to have to post back to the server and do server side validation, but the available validation controls may not always meet my needs:

Quote

1. RequiredFieldValidator - Checks to make sure the user entered a value.
2. CompareValidator - Compares a form field's value with the value of another form field using relations like less than, equal, not equal, etc.
3. RangeValidator - Ensures that a form field's value is within a certain range.
4. RegularExpressionValidator - Makes sure that a form field's value corresponds to a specified regular expression pattern.
5. CustomValidator - Checks the form field's value against custom validation logic that you, the developer, provide.

Descriptions found at 4GuysFromRolla.com - Using the CustomValidator Control

The Need
One such need is to check the length of a string within a field. I know this can easily be done by doing a postback and checking string.length, but I want to avoid the post back.

I know that I could use the CustomValidator and write my own function but I'm slightly too lazy to do even that.

The Problem
After scouring the web looking for a solution to my laziness, I found many forums where users were wanting to use Regular Expressions (RegEx) to validate the length of the string. The answer was always, no it can't be done, no why would you want to do that, etc.

This turned my problem into one of laziness into one of "How can I check the length of a string using regular expressions?" and thus be able to use the RegularExpressionValidator provided to us.

The Solution
To use regular expressions to check the length of a string you need to first decide if the length can be variable but no greater than some number, or if the length has to be exactly some number.

If the length of the string can be between 0 and x (x being any number) then you can use the following expression:

.{0,x}


That will accept any number of characters (alpha, numeric, symbol, etc), up to x long

If you want exactly x characters you can use:

.{x}


In the event that you don't want any character, and perhaps you just want alpha numeric characters instead of a period ".", you can use

[A-Za-z0-9-]


You can plug your regular expression into the ValidationExpression field of the RegularExpressionValidator on your webpage.

Is This A Good Question/Topic? 0
  • +

Replies To: Check Length Of String Using Regular Expression (RegEx)

#2 Guest_J*


Reputation:

Posted 19 April 2010 - 04:36 AM

Problem with

.{x} where x is the number of characters you want to match is that it is a partial check only

say you needed to validate a 5 digit code, your reg exp could be

\d{5}

which would match 12345 as ok, but would also allow the following:

123345
12238476
ghsdkfhk56780-slefkjsl

to force the validation of only 5 digits (enforce the length) use

^\d{5}$

where ^ indicates the beginning of the dataentry field
and $ the end of the entry
Was This Post Helpful? 0

#3 Guest_Hari*


Reputation:

Posted 21 May 2010 - 04:31 PM

How do I write a reg expression to validate a string of length 5 digits and all digits must be less than 3

i.e. combining ^\d{5}$ and [0-3]


Example : 12333 => Valid
12345 => Invalid





View PostJ, on 19 April 2010 - 03:36 AM, said:

Problem with

.{x} where x is the number of characters you want to match is that it is a partial check only

say you needed to validate a 5 digit code, your reg exp could be

\d{5}

which would match 12345 as ok, but would also allow the following:

123345
12238476
ghsdkfhk56780-slefkjsl

to force the validation of only 5 digits (enforce the length) use

^\d{5}$

where ^ indicates the beginning of the dataentry field
and $ the end of the entry

Was This Post Helpful? 0

#4 ShadoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-April 12

Posted 06 April 2012 - 10:06 AM

View PostHari, on 21 May 2010 - 04:31 PM, said:

How do I write a reg expression to validate a string of length 5 digits and all digits must be less than 3

i.e. combining ^\d{5}$ and [0-3]


Example : 12333 => Valid
12345 => Invalid

This probably isn't needed anymore, but since I stumbled across this thread while looking for some help..

^[0-3]{5}$

is probably the answer.. not sure how good of an answer it is, but it works.

- Sorry for bringing up a old thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1