Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 23rd, 2009, 11:42 AM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular expression to validate Digit alphabet DigitDigit

Hi,

I need a regex to allow the user to insert type in a text box strings that maches this pattern:

DigitAlphabet(upper case)DigitDigit i.e the length of the input must be 4 and it must consist of a number a and alphabet number number for example:

2R67 or 0K98 or 3W77

but not 2r67 or ok98 or 2w77

many thanks for your help
  #2 (permalink)  
Old May 23rd, 2009, 12:13 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

HEllo.. Since there are only 4 chars, isn't faster to just check the char one at a time???

to check if one is uppercase, match the char with the same char.uppercase.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #3 (permalink)  
Old May 23rd, 2009, 07:21 PM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by gbianchi View Post
HEllo.. Since there are only 4 chars, isn't faster to just check the char one at a time???

to check if one is uppercase, match the char with the same char.uppercase.
Thanks gbianchi for your reply,

If i write a function to check each charector one by one, how can i use it with RegularExpressionValidator control.

If had the regExp I asigned ValidatorExpression to the RegExp pattern.

Thanks
  #4 (permalink)  
Old May 23rd, 2009, 07:26 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

you can't. If you want to use the control you need the reg exp. You never said you need it for the control, So i didn't assume that.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #5 (permalink)  
Old May 23rd, 2009, 08:27 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

When I'm building regular expressions, I always find the cheat sheet at regexlib.com handy - http://www.regexlib.com/CheatSheet.aspx Also their tester is quite useful - http://www.regexlib.com/RETester.aspx
For your problem you need to match numbers (\d) and upper-case letters ([A-Z]). Also to ensure they only enter 4 characters you want to add start and end markers round it (^ and $). So your expression becomes:
Code:
^\d[A-Z]\d\d$
HTH
Phil

PS. You could use a separate function to do your own check by using a CustomValidator, but it's somewhat overkill to shave off a couple of nanoseconds of processing time

Last edited by philip_cole; May 23rd, 2009 at 08:31 PM..
  #6 (permalink)  
Old May 26th, 2009, 06:11 AM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Philip,

First thanks for your help.
I tried your regex code but the problem is in the css file i have set the text box text uppercase and this casuses the vilidation error message to fire up.

I need to set the textbox text to upper so that the user does not need to press Shift Key.

Any idea how this can be sorted.

Thanks


Quote:
Originally Posted by philip_cole View Post
When I'm building regular expressions, I always find the cheat sheet at regexlib.com handy - http://www.regexlib.com/CheatSheet.aspx Also their tester is quite useful - http://www.regexlib.com/RETester.aspx
For your problem you need to match numbers (\d) and upper-case letters ([A-Z]). Also to ensure they only enter 4 characters you want to add start and end markers round it (^ and $). So your expression becomes:
Code:
^\d[A-Z]\d\d$
HTH
Phil

PS. You could use a separate function to do your own check by using a CustomValidator, but it's somewhat overkill to shave off a couple of nanoseconds of processing time
  #7 (permalink)  
Old May 26th, 2009, 06:57 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

I don't think you can really force it as uppercase, unless you do some javascript like:
Code:
<input type="text" value="" name="thecode" onkeyup="this.value = this.value.toUpperCase()" />
but even that isn't foolproof

If the codes are always going to be uppercase, its probably easier and more reliable, as you aren't forcing the user to enter it correctly, to make the regexp case insenitive and change the value to uppercase in the back end, e.g. in the page code or your database insert.

Phil
  #8 (permalink)  
Old June 3rd, 2009, 05:32 AM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular expression to validate Digit alphabet DigitDigit

Hi Phili,

first thanks for your help with this issue.

Now I have added
Code:
<input type="text" value="" name="thecode" onkeyup="this.value = this.value.toUpperCase()" />
into the textbox control but now the regEx does not fire up i.e

any idea?

Thanks
Rao

Quote:
Originally Posted by philip_cole View Post
I don't think you can really force it as uppercase, unless you do some javascript like:
Code:
<input type="text" value="" name="thecode" onkeyup="this.value = this.value.toUpperCase()" />
but even that isn't foolproof

If the codes are always going to be uppercase, its probably easier and more reliable, as you aren't forcing the user to enter it correctly, to make the regexp case insenitive and change the value to uppercase in the back end, e.g. in the page code or your database insert.

Phil
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression balajinrs Visual Studio 2005 0 July 24th, 2008 02:11 AM
i want to validate my checkbox by using regular ex prabhu66 ASP.NET 1.0 and 1.1 Basics 1 May 24th, 2007 06:50 AM
Regular Expression in C++ praveenholal C++ Programming 1 March 4th, 2007 01:18 AM
Regular Expression Help anshul PHP How-To 4 December 8th, 2004 05:19 AM
help in regular expression diby Beginning PHP 1 September 17th, 2003 12:25 PM



All times are GMT -4. The time now is 05:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.