Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    22
    Rep Power
    0

    Question Javascript regular expression


    Hi!
    i want to test for this input:
    [an optional negative sign] 2 digits [an optional . and an optional digit] like this -34 or -34.5 or 34.5

    This is what i came up with but it doesn't work!
    /^(-)\d{2}(\.d\{1})$/

    Can someone please help me?
    Thanks,
    Paul
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2791
    This is what your regex does: '-xx' + '.x'. You are telling it that it has to have a minus symbol followed by 2 digits, then a period symbol followed by 1 digit. What you need is to tell it to look for an optional minus symbol, followed by 2 digits, followed by an optional period and optional 1 digit.

    /^-?\d{2}(\.\d)?/

    Comments on this post

    • bals28mjk agrees
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  4. #3
  5. Person
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2007
    Location
    Moon
    Posts
    1,158
    Rep Power
    550
    Edit: PHP-Newb (aka Winters) got it, my code requires an end decimal.

    I think your forgetting to escape the digit character.

    ^-?\d{2}\.\d{1}$
    Last edited by bals28mjk; October 30th, 2008 at 07:12 AM.
    Give Opera some love. Use it and you'll see why.

    Oh, those monkeys.::What's wrong with the world today?::The lion king in 5 seconds.

    My body is so sexy.
  6. #4
  7. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2791
    Oh yeah, didn't see that. His expression won't work though; he is demanding a single digit at the end of the expression when he only MIGHT want one. Too be honest he should make up his mind

    Comments on this post

    • bals28mjk agrees : I actually missed your first post. :)
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    22
    Rep Power
    0
    Originally Posted by Winters
    Oh yeah, didn't see that. His expression won't work though; he is demanding a single digit at the end of the expression when he only MIGHT want one. Too be honest he should make up his mind
    Well the user might want to type in -23 degrees, 25.3 degrees or -99.9 degrees.

    I pasted both examples above in an online regex validator. Winters regex didn't match anything and your expression only matched negative numbers with decimals, nothing else.
    Still haven't hit on the righ one
  10. #6
  11. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,126
    Rep Power
    4304
    Which regex validator? Some of them are buggy.

    Try this expression:

    /^-?\d{1,3}(\.\d)?$/


    And yes, I did change the range. I don't see why you don't want to allow single or triple digit numbers.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    22
    Rep Power
    0
    Originally Posted by Kravvitz
    Which regex validator? Some of them are buggy.

    Try this expression:

    /^-?\d{1,3}(\.\d)?$/


    And yes, I did change the range. I don't see why you don't want to allow single or triple digit numbers.
    The program accepts input from a form textfield. The numbers are for degrees celsius. So there might be a negative value and there might not. Yes there could be a single digit. or a single digit and a decimal value.

    The latest regx didn't work either. No numbers were aaccepted.

    http://www.regextester.com/

    If you know of a better one. Please post the URL
  14. #8
  15. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,126
    Rep Power
    4304
    Did you remove the two forward slashes before putting it into the tester?

    I don't bother using a tester script. I just test it in Firebug's Console.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    22
    Rep Power
    0
    Ok I removed the forward slashes and changed to a better online validator(one I could better understand!). I changed the {1,3} to {1,2} to give me the results I wanted. I don't think there is anywhere on earth where the temperature would reach 100 degrees Celsius!

    I believe I need to put the forward slashes back when set the regx = to a variable won't I? But I found this at:

    http://www.xul.fr/javascript/tutorial/regular-expression.php

    var er = new RegExp("xyz", "i")

    would this work if did this:
    Code:
    var er = new RegExp("^-?\d{1,2}(\.\d)?$")
    
    if(! er){ alert( Invalid Entry!)}
  18. #10
  19. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,126
    Rep Power
    4304
    You didn't specify Celsius initially. I usually think of temperature in Fahrenheit.

    You can do it like this:
    Code:
    var er = new RegExp("^-?\d{1,2}(\.\d)?$");
    or this:
    Code:
    var er = /^-?\d{1,2}(\.\d)?$/;
    you would then use it like this:
    Code:
    var str = '50.3';
    if(!er.test(str)){ alert( Invalid Entry!)}
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    22
    Rep Power
    0
    Ok I gotcha! Was my code wrong, or was yours just better coding practice?

    This will be great I can put it in a validating function and call it right after I code the form in HTML.
  22. #12
  23. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,126
    Rep Power
    4304
    Yours wouldn't do anything because a RegExp converted to a Boolean value will always be true and when you negate that it means that the body of that if-condition will never be executed.

    I recommend you read Beware of JavaScript semicolon insertion
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    22
    Rep Power
    0
    Originally Posted by Kravvitz
    Yours wouldn't do anything because a RegExp converted to a Boolean value will always be true and when you negate that it means that the body of that if-condition will never be executed.

    I recommend you read Beware of JavaScript semicolon insertion
    Yeah, I read that and I always use semicolons in my scripts. I just threw that together in order to ask the question.

    I'll save your example to a text file so that I will have it handy, as I modify my program. I originally wrote it in PHP but wanted to make a JS version so I wouldn't have to always go to localhost. I have an old-fashioned sling psychrometer(wet-dry bulb) that I use to measure humidity. The themometers read in degrees Celsius.

    Even in PHP, it's still not idiot proof and needs some more work. I put it on my server. Here's the URL if you want to check it out. It looks like it's supposed to look in Firefox and Netscape, It's too big and slightly off in IE. I haven't tested it in Opera.
    http://www.handleys.us/rh.php
  26. #14
  27. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,126
    Rep Power
    4304
    It looks about the same in Firefox and IE to me.

    You test in neither Safari nor Google Chrome? (They use the same engine -- Webkit -- but the latter starts up quicker in WinXP.)
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  28. #15
  29. Person
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2007
    Location
    Moon
    Posts
    1,158
    Rep Power
    550
    Not Kravvitz!

    Originally Posted by p3aul
    your expression only matched negative numbers with decimals, nothing else.
    It matches positive numbers with a decimal place too. The regex tool you were using before looked fine to me - it doesn't ask for delimiters.
    Give Opera some love. Use it and you'll see why.

    Oh, those monkeys.::What's wrong with the world today?::The lion king in 5 seconds.

    My body is so sexy.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo