Javascript validate number

Javascript validate number

this example allow only number character in you text field. This javascript validate number code can be tested at here .

—- JavaScript validate number code

function numeralsOnly(thisComp, e, isDecimal) {
	var key;
	var keychar;
	if (window.event) {
		key = window.event.keyCode;
	} else if (e) {
		key = e.which;
	} else {
		return true;

	// check double period/decimal/point
	if (((thisComp.value).indexOf('.') > -1) && key == 46) {
		alert("Double period is not allow for this field.");
		return false;

	var str = '0123456789';
	str += isDecimal ? '.' : '';
	keychar = String.fromCharCode(key);
	if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13)
			|| (key == 27)) {
		return true;
	} else if (((str).indexOf(keychar) > -1)) {
		return true;

	alert("Enter numerals only in this field.");
	return false;

——- html code

<input type="text" id="txtSkuPrice"  onkeypress="return numeralsOnly(this,event,true);"/>

numeralsOnly(this,event,true) -> for Double number use true and for integer number use false.


javascript convert string to number

Javascript convert string to number

Some time we need to get number value from currency format string . This Javascript convert string to number post will show how we get the exact number value. This javascript convert string to number code can be tested at here .

Lets consider we have a string its value like $1,221.22. So the actual number is 1221.22 after eliminate the Dollar sign and comma. The javascript code is –

function getNumberFromCurrencyString(currencyString){
var result = currencyString.replace('$',''); //remove dollar
result = result.replace(/,/g,''); //remove comma
return result;

Image Reflection by javascript

I have made one reflection on image using neondragon’s javascript code.

Image reflection by javascript test

Image reflection by javascript test

here i gave the source code of javascript and html file also. for adding reflection you have to add just the class value. here i used class=”reflect”. if your image alreday has class vaule so, you can add reflection separated by space like class=”imageBox reflect”

<img  class=”reflect” style=”width: 384px; height: 276px;”  alt=”” title=”sweet baby” src=”babypic.jpg”>

if you want to change this class name just edit addReflections function156 line from reflection.js

var rimages = document.myGetElementsByClassName(‘reflect’);

i put the default height and opacity in the reflection.js file you can change it  or dynamically it can be change this way – class=”reflect rheight60 ropacity45″

—————————–  Reflection test.html

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
content=”text/html; charset=ISO-8859-1″
<title>Reflection test</title>

type=”text/javascript” src=”reflection.js”></script>

<img class=”reflect” style=”width: 384px; height: 276px;” alt=”” title=”sweet baby” src=”babypic.jpg”>