Created
February 26, 2013 11:39
-
-
Save shahalom/5037867 to your computer and use it in GitHub Desktop.
validating numeric or number values from form element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This example required jQuery library. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* @author: Shah Alom | |
* @website: http://tutpub.com/ | |
*/ | |
//Check Number Value. For ex: 123.10 or +123.10 | |
jQuery.fn.isNumber = function(){ | |
str = $(this).val(); | |
var myPattern = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;// Check if string is numeric +123 | |
if( String(str).search(myPattern) == 0 ) { | |
console.log( str+" is number" ); | |
return true; | |
} else { | |
console.log( str+" is not number" ); | |
return false; | |
} | |
} | |
//Check Number Value. Fro ex: 123 | |
jQuery.fn.isNumeric = function(){ | |
str = $(this).val(); | |
var myPattern = /^\d+\s*$/; | |
if( String(str).search(myPattern) == 0 ) { | |
console.log( str+" is numeric" ); | |
return true; | |
} else { | |
console.log( str+" is not numeric" ); | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Check Currency Value using jQuery</title> | |
<script src="jquery-1.8.3.js" type="text/javascript"></script> | |
<script src="digit.validation.js" type="text/javascript"></script> | |
<script type="text/javascript"> | |
jQuery(document).ready(function(){ | |
$("form").submit( function () { | |
var isError=false; | |
if( $($(".number")).isNumber() == false ) { | |
alert("Invalid Number Value"); | |
isError=true; | |
} | |
if( $($(".numeric")).isNumeric() == false ) { | |
alert("Invalid Numeric Value"); | |
isError=true; | |
} | |
if(isError==true) { | |
return false; | |
} else { | |
alert("TODO"); | |
} | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div style="clear:both; width:500px; max-height:50px; display:block; margin:0 auto;" class=""> | |
<form> | |
Number Value: <input type="text" name="currency" class="number" value="" /><br /> | |
Numeric Value: <input type="text" name="numeric" class="numeric" value="" /><br /> | |
<input type="submit" value="Submit" /> | |
</form> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment