On Dec 17, 2:12 am, kangax <kan...@gmail.com> wrote:
> Michael wrote:
> > This is awesome - I am totally almost getting this!
>
> > What if my Coupon Code started at some number mid sequence - like
> > everything
> > greater then
>
> > C32456 would be valid...
>
> It's almost always a pain to work with numeric ranges in regex. I
> think the best option in this case is to parse sequence as a number
> and just use comparison operators. Something like:
>
> var passed = false;
> var match = 'C32456'.match(/\d+/);
> if (match && match[0]) {
>   match = parseInt(match);}

Will that result differ from:

  var match = parseInt('C32456');


>
> if (!isNaN(match)) {
>  passed = (match > 1234);

There is no need to use parseInt.  During evaluation of the '<'
operator, if one expression is a Number, the other will be converted
too.  A simpler algorithm finds the number part by simply removing
leading non-digits, so:

  var s = 'C32456';
  passed = s.replace(/^\D+/,'') > 1234;


A RegExp test should probably be done first to ensure the correct
format before testing that the number part is in range, so something
like the following may do:

  if (/^C\d+$/.test(s) && s.replace(/^\D+/,'') > 1234) {
    // coupon OK
  }

adjusted for whatever formats need testing.  If there are many
formats, a function could be written to test the format (passed as a
RegExp) and that the number part is within a certain range, something
like:

  function testCoupon( s, formatRE, min, max) {
    var n = s.replace(/^\D+/,'');
    return re.test(s) && n >= min && n <= max;
  }

 and

  testCoupon('C123', /^C\d+$/, 0, 1000)


--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to