In my current app, I am implementing field validation with regex for forms.
More specifically, I am having trouble handling edge-case for numbers with decimals.
Essentially I am using regex to prevent users from typing wrong information in to the input fields.
For instance, if user would type 12.2 and then . afterwards, I am using regex to detect what shouldn't be there, and replace with empty string, ''
Here's my current implementation using a call back function:
export const checkFormat = (typeFormat, value) => {
//value is stringified
switch (typeFormat) {
//...
case NUMERIC_DECIMALS: {
return value.replace(/(\d+\.\d+)(\.*)\d*/g, '$1')
}
}
}
However, the current regex implementation can't handle such cases as
- User types :
., then..==>. - User types :
123.2, then1.23.2==>1.232
I'm fairly new to Regex, so obviously it needs some work
1.00sdsdsd = 1.00