I have a numeric type input field on an app on react-native.
This field must accept comma numbers only.
So I used:
keyboardType="numeric"
But in this case it is still possible to use characters such as point, space and line (-). In addition to any errors in entering incorrect number data.
The correct format should be this: nnn,ddd
Can you give me a hand?
let a = [
"0",//0
"0,5",//0,5
"0,,5",//0,5
"0.5",//0,5
"0..5",//0,5
".0.5",//0,5
",0,5",//0,5
"0.-5,",//0,5
"0,-5.",//0,5
"0,5-..",//0,5
"0,,,- 5,,",//0,5
"0...5- ",//0,5,
"0,5,"//0.5,
];
a.map(el=> {
const e = el
.replaceAll('.', ',')
.replace(/[^0-9,]/g, '')
console.log(e)
});
Edit:
let a = [
"0",//0
"0,5",//0,5
"0,,5",//0,5
"0.5",//0,5
"0..5",//0,5
".0.5",//0,5
",0,5",//0,5
"0.-5,",//0,5
"0,-5.",//0,5
"0,5-..",//0,5
"0,,,- 5,,",//0,5
"0...5- ",//0,5,
"00,5",//0,5
"00,05",//0,05
"05,512",//5,51
"00,5123",//0,51
",05",//0,05
"00,,5",//0,5
"21,"//21
];
- There cannot be two leading zeros: "00,5",//0,5 "00,05",//0,05
- There must be no leading zero if it is then followed by a number without first having a comma: "05,512"//5,51
- After the comma there must be only two digits "00,5123"//0,51
- Last but I think you can also ignore this case, if the field is empty a comma or a point is put, at the beginning a zero is put followed by a comma: ",05",//0,05