just use regex
$('#formelement').val().match(/[-+]?[0-9]*\.?[0-9]+/)
(excluding the selector, everything else is plain javascript)
As noted in the comments, since you need to do this for each character inserted, you need to consider the valid decimal part (for example, /[-+]?[0-9]*\.?[0-9]*/ )
Since the people in the comments make me be precise, I can offer you how to work out how to use this mapping for your purpose (but you donโt allow anything to the OPโs imagination :()
You can divide the regular expression into 3 regular expressions, one for the first part (the final character and the integer part), one for the first part plus a dot character and one for the whole number.
The validation procedure should accept the input at the time of writing if it matches at least one of the rules just described three times, and the check performed at the end should only be accepted when matching the last regular expression (since you are sending a value and you need it to it was right)
source share