Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some help

Status
Not open for further replies.

kss444

Programmer
Sep 19, 2006
306
US
I need a reg exp or a javascript that will validate a text box and only allow numbers, dashes and one decimal point.
This script will have to work with IE 7 and 8 and the latest Firefox.

And the user should be able to use the backspace, the arrows alt+key and ctrl+key


Ordinary Programmer
 

2nd link returns this


--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Thanks but I have this code and it works in IE, and somewhat in Firefox.
function check_float(e, field) {
//alert(e.keyCode);
var key;
if (window.event) {
key = e.keyCode;
}
else if (e.which) {
key = e.which;
}
//alert(key);
if (!(((key >= 48) && (key <= 57)) || (key == 45) || (key == 46))) {
//if (!(((key >= 48) && (key <= 57)) || (key == 45) || (key == 46) || (key == 8) || (key == 9) || (key == 13) || (key == 17) || (key == 18) || (key == 19) || (key == 33) || (key == 34) || (key == 35) || (key == 36) || (key == 37) || (key == 38) || (key == 39) || (key == 40) || (key == 45) || (key == 46))) {
alert("Only digits, dashes and one decimal point are allowed!");
return false;
}
if (key == 46) {
var patt1 = new RegExp("\\.");
var ch = patt1.exec(field);
if (ch == ".") {
alert("More then one decimal point not allowed");
return false;

}
}

I need to have the user be able to use their backspace key, and the arrow keys and alt+key and ctrl+key, tab key and a lot of other keys and it works in IE but not in FF. The user cannot use the backspace or arrow keys in FF.

Ordinary Programmer
 
how are you calling the function?



--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
in the page load;
saveNMFCItemNumber.Attributes.Add("onkeypress", "return check_float(event,document.getElementById('" + saveNMFCItemNumber.ClientID + "').value)")



Ordinary Programmer
 
Does something like this work

- built in func to allow only 1 decimal point and the minus sign can only be at the start of the text box.

I.E:

12.0.0 is invalid
12-.3 is invalid


The $key function is something I use all the time.

For some reason, the minus sign (either in the top row on the keyboard or from the numberpad) returns "insert" and similar with the decimal point from both locations returns "delete"

Works in IE6 / FF3 / Chrome

Code:
<input type="text" name="myTbx" id="myTbx" onkeypress = "return checkKey(event)" />
<script>
function checkKey(e) {
	// permit only one decimal point
	if ((document.getElementById("myTbx").value.indexOf(".") > -1) && ($key(e) == "delete")) {
		return false;
	};
	// permit the minus sign only at the start
	if ((document.getElementById("myTbx").value.length > 0) && ($key(e) == "insert")) {
		return false;
	};
	return (!$key(e)) ? false : true;
}
function $key(e) {
	var evt = window.event ? event : e;
	var intAction = evt.charCode ? evt.charCode : evt.keyCode;
	switch (intAction) {
		case 8: return "backspace"; break;
		case 9: return "tab"; break;
		case 13: return "enter"; break;
		case 20: return "caps lock"; break;
		case 27: return "escape"; break;
		case 35: return "end"; break;
		case 36: return "home"; break;
		case 37: return "left"; break;
		case 38: return "up"; break;
		case 39: return "right"; break;
		case 40: return "down"; break;
		case 45: return "insert"; break;
		case 46: return "delete"; break;
		case 48: return "0"; break;
		case 49: return "1"; break;
		case 50: return "2"; break;
		case 51: return "3"; break;
		case 52: return "4"; break;
		case 53: return "5"; break;
		case 54: return "6"; break;
		case 55: return "7"; break;
		case 56: return "8"; break;
		case 57: return "9"; break;
		case 109: return "subtract"; break;
		case 110: return "decimalpoint"; break;
		case 189: return "dash"; break;
		case 190: return "period"; break;
		default: return false; break;
	}}


--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
yea I believe that would work.
Thanks,

Ordinary Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top