0

I was looking for a javascript regex pattern that will allow inserting or registering these type of amount

5
5.1
5.10
5
500,000
500,000.1
500,000.10

but these numbers should not be lesser than 0. I managed to find pattern but I want to omit the "$". All I want is numbers,commas,and dots only

var x = 5000;
var a = "5,000,000"
var y = "$5,,,000,000"
var z = "500,000"
var c = "c231"
console.log(x," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(x))
console.log(a," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(a))
console.log(y," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(y))
console.log(z," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(z))
console.log(c," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(c))
3
  • 1
    Remove \$? From RegEx Commented Jul 27, 2016 at 5:25
  • 1
    New RegEx will be /^\d+(,\d{3})*(\.\d*)?$/ Commented Jul 27, 2016 at 5:26
  • Did my answer help? Consider accepting it if it did. Commented Jul 27, 2016 at 22:49

3 Answers 3

3

Remove the \$?

/^\d+(,\d{3})*(\.\d*)?$/

var x = 5000;
var a = "5,000,000"
var y = "$5,,,000,000"
var z = "500,000"
var c = "c231"
var d = "1...0.00"
var e = "..."
var f = "1.00,,,"

var validRegEx = /^\d+(,\d{3})*(\.\d*)?$/;

console.log(x," Valid", validRegEx.test(x))
console.log(a," Valid", validRegEx.test(a))
console.log(y," Valid", validRegEx.test(y))
console.log(z," Valid", validRegEx.test(z))
console.log(c," Valid", validRegEx.test(c))
console.log(d," Valid", validRegEx.test(c))
console.log(e," Valid", validRegEx.test(c))
console.log(f," Valid", validRegEx.test(c))

Sign up to request clarification or add additional context in comments.

Comments

0

Based on my Comment on Question the new code will be

var x = 5000;
var a = "5,000,000"
var y = "$5,,,000,000"
var z = "500,000"
var c = "c231"
console.log(x," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(x))
console.log(a," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(a))
console.log(y," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(y))
console.log(z," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(z))
console.log(c," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(c))

Comments

0

Try

^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(\.[0-9][0-9])?$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.