Allow Only Numbers And Dot In Script
Answer : This is a great place to use regular expressions. By using a regular expression, you can replace all that code with just one line. You can use the following regex to validate your requirements: [0-9]*\.?[0-9]* In other words: zero or more numeric characters, followed by zero or one period(s), followed by zero or more numeric characters. You can replace your code with this: function validate(s) { var rgx = /^[0-9]*\.?[0-9]*$/; return s.match(rgx); } That code can replace your entire function! Note that you have to escape the period with a backslash (otherwise it stands for 'any character'). For more reading on using regular expressions with javascript, check this out: http://www.regular-expressions.info/javascript.html You can also test the above regex here: http://www.regular-expressions.info/javascriptexample.html Explanation of the regex used above: The brackets mean " any character inside these brackets ." Y...