1

Validations i'm trying :

  • regular expression of : 9999.99 and no spaces (0009.99 it should convert 9.99

Edit :

var regex = '(?!0)\d+(?:\.\d+)?$';

function getValue()  {
    // passing value 0009.99 and 0009.00 and 100
    return document.getElementById("myinput").value;

}

function test() {
    alert(regex.test(getValue()));
}

function match() {
    alert(getValue().match(regex));    
}
Var
  • 270
  • 1
  • 5
  • 23

1 Answers1

4

Your first and second seems to Work just fine, the third can be achieved with the following regex:

/(?!0)\d+\.\d+$/

It starts by looking forward for zeros (skipping them), then it matches any number of digits followed by a dot and more digits. If you want the digits to be optional you can replace the plus '+' with a star '*'.

Edit:

If you want to allow integers, you can use this regex:

/(?!0)\d+(?:\.\d+)?$/

That makes the dot and the digits after that optional.

BTW: Your jsfiddle does not help in answering.

Edit2:

To create a Regex using quotes you must use the following syntax:

var regex = new RegExp('(?!0)\d+(?:\.\d+)?$');

Edit3:

I forgot to mention, you need to double escape backslashes, it should be:

var regex = new RegExp('(?!0)\\d+(?:\\.\\d+)?$');

Now it should Work directly in your code.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57