Page 1 of 1

Help me out for user input ranges from--

Posted: Wed Apr 29, 2009 6:00 am
by vijay1253
Hi am new to regex am in a need of regex for input from

ranges from 1 to 6999 accepting -and space in bettween them.

ex: 1-6999 (true- validation)
6999-6999(true- validation)
6999 6999(true- validation)
6999 (true- validation)
0 (false- validation)
1-7000(flase- validation)

i have written an regex :^([1-9]\d{0,2}|[1-6]\d{0,3})?[- ]?([1-9]\d{0,2}|[1-6]\d{0,3})$

but its not working can any one help me!

Thanks in advance!

Re: Help me out for user input ranges from--

Posted: Wed Apr 29, 2009 6:13 am
by prometheuzz
Regex is not the right tool for this job. Validating if a number does not exceed a certain value, like 6999, is not an ideal task in regex and numerically comparing two values is even worse.

You should solve this using "normal" programming logic. That way it's easier to write and you (and others reading your code) will be able to understand it.

Often regex can solve a certain validation is less code than "normal" programming logic, but this is absolutely not the case in this instance. Really.

Re: Help me out for user input ranges from--

Posted: Wed Apr 29, 2009 6:38 am
by vijay1253
actually for understanding i just kept (true validation)

as per my requierment i need it in regex...

i need a validator type of regex...

accepting the input value as 1-6999 or 1 or 1 6999

i.e min value is 1 and maximum value if 6999.

can u please give me regular expression fro this!

Re: Help me out for user input ranges from--

Posted: Wed Apr 29, 2009 8:52 am
by prometheuzz
vijay1253 wrote:actually for understanding i just kept (true validation)

as per my requierment i need it in regex...

i need a validator type of regex...

accepting the input value as 1-6999 or 1 or 1 6999

i.e min value is 1 and maximum value if 6999.
Just to be sure I understand you, you also want string like this to be rejected: "1234-1233" (ie. a larger number before a smaller one), right?
If that is correct, then again: this is very impractical in regex. If you do cook up a solution for this, it will be a horrible monster of a regex (very long and very complicated). Again: you don't want to do it. If it is a requirement, you should go back to the person who decided it should be done using regex and tell him or her it can't be done. Period.
vijay1253 wrote:can u please give me regular expression fro this!
No.

For future posts, please take the time to write a clear message. Your messages are hard to read because of the many errors in them and the lack of capital letters at the start of a sentence and all the unnecessary full stops after your sentences. Don't get me wrong, you don't have to spell perfectly, but it is clear by the misspelled words like "requierment" (requirement), "fro" (for) and "u" (you) you simply didn't take the time to reread your post before submitting it.

Thank you.

Re: Help me out for user input ranges from--

Posted: Thu Apr 30, 2009 1:39 am
by vijay1253
I am very much thankfull to you in correcting me!

As i am new to forums i am committing mistakes defiantly i will try to improve.

I need to accept numbers from 1 to 6999 in any of the formate i.e first can be greater than or vise versa.

But in between two values - or space are acceptable or single value is also acceptable.

Can you write regex for this!

Re: Help me out for user input ranges from--

Posted: Thu Apr 30, 2009 5:19 am
by prometheuzz
vijay1253 wrote:I am very much thankfull to you in correcting me!

As i am new to forums i am committing mistakes defiantly i will try to improve.
Thank you.
vijay1253 wrote:I need to accept numbers from 1 to 6999 in any of the formate i.e first can be greater than or vise versa.
Ah, then regex can be used (still "normal" programming logic is still preferred, but it can be done quite easily in regex nevertheless).
vijay1253 wrote:But in between two values - or space are acceptable or single value is also acceptable.

Can you write regex for this!
Well, I can help you write it, okay? Could you post what you have tried yourself and indicate what isn't working for you? I'm sure I can explain the error in your logic and point you towards the solution.

Good luck!

Re: Help me out for user input ranges from--

Posted: Tue May 05, 2009 2:39 am
by vijay1253
Hi thank you for your support!

I have written a regex in my php code

Code: Select all

[trid]
label = Trader 
validator.type = RegExp
validator.expression = "^([1-9]\d{0,2}|[1-6]\d{0,3})?[- ]?([1-9]\d{0,2}|[1-6]\d{0,3})$"
editor.editable = "true"
editor.type = TextInput
required = "true"
But when i am validating the input it is accepting
6999-69999 //Out of range
0 //Sholud not accept
6999 //Here is accepting fine
6999- //Here is accepting fine

But as per my requirement it should accept
here min value is 1 and max value is 6999 for both first and second values
1-6999 or 6999-6999 //First value can be greater or less than the second value & vise versa for second value.

And i need one more regex for the 6999,32767
Here min value for first value & second value is 1
and the max value is 6999 for first and 32767 for second.
Condition: Both the values can be accepted by ","(comma) in between or single value is also accepted.

Can you correct my first regex and guide me for the second regex.

I am very much thankful if you write the regex for me.

Thanks in advance!

Re: Help me out for user input ranges from--

Posted: Tue May 05, 2009 3:07 am
by prometheuzz
vijay1253 wrote:Hi thank you for your support!

I have written a regex in my php code

Code: Select all

[trid]
label = Trader 
validator.type = RegExp
validator.expression = "^([1-9]\d{0,2}|[1-6]\d{0,3})?[- ]?([1-9]\d{0,2}|[1-6]\d{0,3})$"
editor.editable = "true"
editor.type = TextInput
required = "true"
But when i am validating the input it is accepting
6999-69999 //Out of range
0 //Sholud not accept
6999 //Here is accepting fine
6999- //Here is accepting fine

But as per my requirement it should accept
here min value is 1 and max value is 6999 for both first and second values
1-6999 or 6999-6999 //First value can be greater or less than the second value & vise versa for second value.
This regex will validate those ranges:

Code: Select all

^([1-9]\d{0,2}|[1-6]\d{1,3})([- ]([1-9]\d{0,2}|[1-6]\d{1,3}))?$
vijay1253 wrote:And i need one more regex for the 6999,32767
Here min value for first value & second value is 1
and the max value is 6999 for first and 32767 for second.
Condition: Both the values can be accepted by ","(comma) in between or single value is also accepted.

Can you correct my first regex and guide me for the second regex.

I am very much thankful if you write the regex for me.

Thanks in advance!
This is what I previously meant that regex is not well suited for numerical validating. To validate a number is it's between 1 and 32767, you will have to write this lengthy regex:

Code: Select all

^([1-9]\d{0,3}|[1-2]\d{4}|3[01]\d{3}|32[0-6]\d{2}|327[0-5]\d|3276[0-7])$
A short explanation:

Code: Select all

[1-9]\d{0,3}     // range 1-9999
|                // OR
[1-2]\d{4}       // range 10000-29999
|                // OR
3[01]\d{3}       // range 30000-31999
|                // OR
32[0-6]\d{2}     // range 32000-32699
|                // OR
327[0-5]\d       // range 32700-32759
|                // OR
3276[0-7]        // range 32760-32767
Now see if you can finish that second validation yourself.

Good luck!

Re: Help me out for user input ranges from--

Posted: Tue May 05, 2009 7:47 am
by vijay1253
Hey Thank you!

Thanks a lot! Regex is working fine and even i got to know how to write for the second one.

Really you saved my time a lot. Actually i was trying to work out from a long time at last i made it!

Anyways Thank you!

May i know your good name please.

Re: Help me out for user input ranges from--

Posted: Tue May 05, 2009 7:52 am
by prometheuzz
vijay1253 wrote:Hey Thank you!

Thanks a lot! Regex is working fine and even i got to know how to write for the second one.

Really you saved my time a lot. Actually i was trying to work out from a long time at last i made it!

Anyways Thank you!

May i know your good name please.
Good to hear it, and you're welcome.

Sure, you can know my name: it's Bart.