Developing a simple validation library in JavaScript: continued - Validation for a number (both integers and floating point numbers, either positive or negative)
(Page 4 of 4 )
In the previous sections, we worked on validating multi-digit numbers. Now, it is time to work with floating point numbers (or real numbers). Let us look at the following code:
<html>
<head>
<script id="clientEventHandlersJS" language="javascript">
<!--
function isValidNumber(val){
if(val==null){return false;}
if (val.length==0){return false;}
var DecimalFound = false
for (var i = 0; i < val.length; i++) {
var ch = val.charAt(i)
if (i == 0 && ch == "-") {
continue
}
if (ch == "." && !DecimalFound) {
DecimalFound = true
continue
}
if (ch < "0" || ch > "9") {
return false
}
}
return true
}
function Button1_onclick() {
var v = document.all("txtNumber").value;
alert(isValidNumber(v));
}
//-->
</script>
</head>
<body>
<form id="form1">
Enter Number:<input type="text" id="txtNumber" NAME="txtNumber"> <input type="button" value="Validate" id="Button1"
name="Button1" onclick="return Button1_onclick()">
</form>
</body>
</html>
The new function from above is defined as follows:
function isValidNumber(val){
if(val==null){return false;}
if (val.length==0){return false;}
var DecimalFound = false
for (var i = 0; i < val.length; i++) {
var ch = val.charAt(i)
if (i == 0 && ch == "-") {
continue
}
if (ch == "." && !DecimalFound) {
DecimalFound = true
continue
}
if (ch < "0" || ch > "9") {
return false
}
}
return true
}
The above function is slightly different from the functions in the previous sections. In the case of a negative number, we need to test for the negative symbol (or hyphen) only at the first position as follows:
if (i == 0 && ch == "-") {
continue
}
But the decimal point could occur at any location, and that too can occur only once. As I need to test for a “once occurrence”, I declared a flag variable (“DecimalFound” of type Boolean). Initially, I start with “DecimalFound” as false (which means that no decimal is found). I update it to “true” once it finds the decimal point. If it again finds a decimal point (when “DecimalFound” is true), it returns false automatically based on the logic defined above.
If all of the functions in this series are kept in a single JavaScript file, we can even reuse the functions within another. This reduces the code and maintenance. But you need to keep in mind that unnecessary code also gets emitted if you don’t use all the functions in your web page.
Any comments, suggestions, ideas, improvements, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |