• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

JavaScript RegEx: Numeric Only

I need a bit of JS that will discard the entry of any non-numeric character.

Because this is a lot number value, it *will* have leading zeroes, and may *not* have signs or decimal points or commas.  Literally, just 0-9 only.

I wouldn't mind knowing how to add in A-Z or a-z also, later on, in case they decide that alpha is okay.

Order/sequence would never be important, nor would total length of the value.

Thanks!
0
bamapie
Asked:
bamapie
  • 5
  • 5
  • 3
  • +1
2 Solutions
 
farzanjCommented:
RE would be:
^[0-9]+$
0
 
farzanjCommented:
For alpha-numeric, you may use
^[0-9a-zA-Z]+$

0
 
bamapieAuthor Commented:
So, in JS, are we talking about:

         oElement.value = oElement.value.replace(^[0-9]+$, '');

and

         oElement.value = oElement.value.replace(^[0-9a-zA-Z]+$, '');


?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
farzanjCommented:
No, you are just matching NOT replacing if I understand your correctly.

So in that case you have either
 re = /^[0-9a-zA-Z]+$/

Or
re = new RegExp("^[0-9a-zA-Z]+$")

0
 
farzanjCommented:
This is the kind of code I would be using

if (myString.match(/^[0-9]+$/)) { /*Success!*/ }.
0
 
bamapieAuthor Commented:
Per first line of my question, I need to discard.  Thanks.
0
 
farzanjCommented:
So you need to match not replace.  So you test match.
0
 
bamapieAuthor Commented:
So are we talking about this?  I'm having to guess here.

if (oElement.value.match(/^[0-9]+$/)) {
   oElement.value = oElement.value.replace(^[0-9]+$, '');
}

I'm trying to come up with the JS I asked for using the clues I have from you.

If this is still not correct, let's pause and allow other folks to answer.
0
 
käµfm³d 👽Commented:
Perhaps this?

function checkValue(ctrl)
{
	ctrl.value = ctrl.value.replace(/\D/, "");
}

Open in new window


<input type="text" onkeyup="checkValue(this);" />

Open in new window

0
 
sentnerCommented:
If you do a replace on that, you're removing all the numbers, not removing the non-numbers.   I'm assuming you want to keep all the numbers and remove anything else, yes?  Does this mean that if you have a line that looks like "sy3fh089" you want it to come out as "3089", or do you want it to simply fail on or ignore the input?  

The "match" that farzanj is referring to will fail on that example, and will only "succeed" on a valid number-only entry.

The portion in brackets is what you want to look for.  The "^" and "$" refer to the beginning and ending of the string.  However you can put the ^ inside the bracket to reverse the match (make it a "not").   If you want to strip out all the non-numeric characters and only keep the numbers, then you can do so with something like:

oElement.value = oElement.value.replace([^0-9], '');

This should remove anything from the string which is not a numeric character.  


http://www.omicentral.com/cheatsheets/JavaRegularExpressionsCheatSheet.pdf
0
 
sentnerCommented:
Sorry, forgot to make that global, or it will only work against the first char.  


oElement.value = oElement.value.replace(/[^0-9]/g, '');
0
 
käµfm³d 👽Commented:
\D  ==  [^0-9]

= D
0
 
käµfm³d 👽Commented:
Nevermind. I thought that was directed to me  : \
0
 
bamapieAuthor Commented:
sentner:

>if you have a line that looks like "sy3fh089" you want it to come out as "3089"

That's exactly right.  I'll try your proposal tomorrow or so.

Thank you!
0
 
bamapieAuthor Commented:
kaufmed:  I'll also try yours tomorrow.  Just noticed your post.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now