jquery - Javascript numeric input (regex) validation rules -
I got some stuck here.
Regarding input of accounting data, the analyst has the rules on the input of decimal data in a specific set text box.
Since I have not studied regular expressions at this point, and because I have time limits for presenting a lot of work, I request you to help.
The rules are (on the blur):
- IE8 + compatible;
- Set the default on 2 digits behind commas;
- Reject ".", "," And other characters compared to numerical characters;
- If the returned number has multiple separators, then only keep it for the last time.
Clarification:
Employees can have different regional settings, and commas / dot can be used either decimal or thousand separators.
If an employee pastes a copy - thousand and a decimal separator, then it has to be ignored.Numeric '). ('Staining', function () {var mystring = $ ('# test_rules.numeric'). Val (); myString = parseFloat (myString.replace (/ [^ \ D.- .-] / g, '')) ; MyString = toFixed (myString, 2); console.log (myString);}); Fixed Fix (value, exact) {var power = Math.pow (10, Precision || 0); Return string (Math (value * power) / power); }
The regular expression used does not work correctly because it only accepts the dot, not a comma.
I do not even know how I should do this so that only the last separator remains (so that thousands of divisors are ignored)Try this function:
function parseInputNum (val) {var Sep = val.lastIndexOf ('.') & Gt; Val.lastIndexOf (',')? '.' : ','; Var arr = val.replace (new 'RegExp (' [^ \\ d '+ sep +'] + ',' g '),' '). Match (new regx ('(\\ d + (?: [' + Sep + '] \\ d + |)) $')); Return arr? Arr [1] .replace (/ [,] / g, '.'): Wrong; }
Comments
Post a Comment