• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with number format issue

 
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Best, ranchers!

I wonder if anyone could help me with a number formatting issue that i have dealt with for quite some time now.
I wan't to format an input value from a textfield to a decimal pattern as the user types the digits in the textfield.
Also i want to limit the input value to 10 so the range would be from 0.0, 0.1, 0,2 (and so on ... ) - having a step
size of 0.1 up to 10. The input value should be set back to 10 if the user enters a value of 11 or higher in the textfield.

I don't wan't to use the component <input type="number" /> from HTML 5 in this case.

I know a procedure like the above requires some regular expressions where hence i am a beginner. So far i have
this very, very little sample code:



Thank you so much in advance and very kind regards,
Robert!


 
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A regex for that might be \d (?:\.\d)?    

Something like that
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this not something you can simply use pattern for?
 
Robert Ingmarsson
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Is this not something you can simply use pattern for?



At the moment im in a pre-google state when it comes to this answer. Thank you so much by the way. But how could a
pattern be used as an example in this case? I am a beginner when it comes to regular expressions, i am aware of them
and know how they works and know only some basic letter oriented syntax. Has a pattern something to do with regex?
Thank you so much for your replay to this post, Mr Bibeault.

Kind regards,
Robert!
 
Robert Ingmarsson
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:A regex for that might be \d (?:\.\d)?    

Something like that



Thank you so much for your reply. I shall test it. But the input number in the textfield should be limited to 10.
Can you accomplish this in the same regex string?

Kind regards,
Robert!
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pattern bear was referring to was the pattern attribute.  You put the regex in there.     That regex I gave you wont allow 10
You can do this: (?:10|\d (?:\.\d)?)
The extra 10 in there looks a bit hacky though
 
Robert Ingmarsson
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:The pattern bear was referring to was the pattern attribute.  You put the regex in there.     That regex I gave you wont allow 10
You can do this: (?:10|\d (?:\.\d)?)
The extra 10 in there looks a bit hacky though



Best, Mr Hobbs!

When i look at the syntax of the regex you wrote I can see what it is doing, but how do I use it in a working example?
I have tried the following:



But nothing seems to work. I have read that every regex must start with a "/" and be terminated with "\" so i'm not sure
about parenthesis in a if statement. Could you show me what i am doing wrong?

Very kind regards,
Robert!

 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are better off NOT trying to test the entire field value in a keyup event handler.

The keyup should check for a key value between '0' and '9' OR '.' OR '-' (if negative numbers are allowed, '+' optionally) OR RETURN (without this, the user cannot tab out of the field, but don't store it). Any other keystroke you could reject and beep if entered. If you want to get even fancier, you can check for an existing "." in the current value, and even entry of a sign when the value is not previously empty.

When the control loses focus, the focus-lost event is where you check the entire text field (that handles things like bad pastes), and complain if you don't like the format or value.

In javascript, a regex literal MUST begin and end with slashes (NOT backslash). OR you can make one from a string, viz:


Note the backslash escape for ".". It's a "magic" regex character when not escaped.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Bear said,  you can use the pattern attribute.   Look up html pattern attribute.  You put the regex as the value and the field will allow the input only if it fits the regex.
 
Robert Ingmarsson
Ranch Hand
Posts: 201
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  I give up. I have tried everything but like in everything else in life i fail. Im a semi rated beginner, not on javascript but on regex,
so i think im going crazy on this one. I can see the solution before me if I close my eyes so clearly. I must look in to more RegExp. I can't make any of the
provided regex on this page work.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:Like Bear said,  you can use the pattern attribute.   Look up html pattern attribute.  You put the regex as the value and the field will allow the input only if it fits the regex.



I don't think that the onkeyup event has added the key just pressed to the control value, so a regex check of the current value would do no good. And it's a pain to try and create the after-insertion value for testing, since you've got to factor in the current input text cursor when doing do. Then, as I said, there's clipboard paste.

Which is why I recommended vetting for legal characters only on keyup, then doing the actual regex check when focus is being lost. This is how I do it and it has been very successful.

Don't despair, Robert. regexes can cause even the most experienced of us to tear our hair out. Fortunately there are websites out there that can let you test-code and try regexes.

And, of course, if you don't like the one I supplied (which admittedly is cobbled from memory and untested), Google should be able to supply others.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic