Only One Dot "." Regex
Solution 1:
Solution 2:
Well, it depends about what is the format that you will accept. Using /^(\d)(.)?(\d)$/ will help you to ensure that there is only one point in the number, but also accepts numbers without decimal point or just the decimal point. Also you can use isNaN() to make sure it is a number.
Hope it works for you.
Solution 3:
Not the most elegant solution, but it checks the input as the user types and only allows a number with a single decimal.
link: function (scope, element, attrs, ngModelCtrl) {
scope.currentValue = '';
element.bind('keyup',function(e) {
if( e.which!=8 && e.which!=0 && e.which!=46 && e.which != 190 && (e.which<48 || e.which>57)) {
element.val(scope.currentValue);
}
if(e.which != 8 && e.which != 0) {
var valToCheck = element.val();
var r = /^(\d*)\.{0,1}(\d*)$/if (!r.test(valToCheck)) {
element.val(scope.currentValue);
} else {
scope.currentValue = element.val();
}
}
});
The problem with this is that you see the user type in a letter, for example, and then you see it disappear. This works much better using "keypress" rather than "keyup" however then you have to take into account the user clicking the mouse somewhere in the middle of the currently inputted value and then typing so it gets tricky.
Solution 4:
I would suggest just testing for more than 1 dot. Example
(string.test(/\d\.\.?(\.+)/gm)) ? //dotrue : //dofalse;
test
checks if it exists in string and then returns true or false.
the regex checks for a digit then two dots or two dots +. It checks globally and multiple lines.
Then if you're insistant on using regex to check for three groups of three digits separated by one dot you can use the regex expression
/(\d{3}\.\d{3}\.\d{3})\b/gm
Example here: http://regexr.com/3auj5
Post a Comment for "Only One Dot "." Regex"