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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Currency TextBox

Status
Not open for further replies.

nerdalert1

Programmer
Nov 4, 2004
92
US
Hello all. I have multiple text boxes that are going to have the user typing in currency values like 5.25. How can I have one validator to handle these multiple textboxes? I don't want to have to use a validator control for each textbox. Thanks all
 
Hi nerdalert1,

When you say validator, I'm assuming you want to check to make sure the text box does not contain the $ sign, or any symbols, Characters, or too many decimal places.

I recommend creating a separate class and calling a function in that class from each page. If you don't want to instantiate a new class on every page, try creating a Singleton (Google For It)

 
Here is something you can try and see if you like it. Copy all of the code below the line into a file and call it something like ValidateCurrency.HTC. This in your control
<input type="text" id="mycurrency" STYLE="behavior:url('ValidateCurrency.HTC')" >

or <asp:textbox STYLE="behavior:url('ValidateCurrency.HTC')" ></asp:textbox>

you can also assign the behavior to a class in a CSS file and just refer to the class in Class ASP but that does not seem to work in ASP.NET?

This will filter the keystrokes so that only numeric keys are accepted and it will insert a comma and $ after you lose focus. You can modify all of that behavior in the htc file if you don't like it.
-------------------------------------

<PUBLIC:COMPONENT lightWeight="true">
<PUBLIC:ATTACH EVENT="onkeydown" ONEVENT="validateInput()" />
<PUBLIC:ATTACH EVENT="onpropertychange" ONEVENT="checkspecial()" />
<PUBLIC:ATTACH EVENT="onfocus" ONEVENT="removedollar()" />
<PUBLIC:ATTACH EVENT="onblur" ONEVENT="validateNumber()" />


</PUBLIC:COMPONENT>


<SCRIPT language="JScript">

var oldValue= ""
var formatflag
var numdecimals = 2
var focusflag
var lastkeydelete
var AllSelected


function removedollar() {

var tempvalue


focusflag = true;
// if (formatflag == false) {
// removecommas()
// formatflag = false;
// }



var theLen = event.srcElement.value.length
tempvalue = event.srcElement.value
if (event.srcElement.value.substring(0,1) == "$")
tempvalue = event.srcElement.value.substring(1,theLen)
// tempvalue = tempvalue.replace(",","")
tempvalue = replacecommas(tempvalue)
event.srcElement.value = tempvalue
element.select()
}



function checkspecial() {
// if $ or , already present then don't format this again.

// if (focusflag == true) return;
if (event.srcElement.value.substring(0,1) =="$") return ;
if (event.srcElement.value.indexOf(",") > 0) return ;
if (event.srcElement.value == " "){
event.srcElement.value = "";
return}

//if non numeric passed don't format for example irregular or n/a for payment amount in OtisWeb
if (checkvalidnumber(event.srcElement.value) == false) return;


if (lastkeydelete == true) return;


if (formatflag==true) {
formatflag = false;
return; }


// if assigning value without focus call validatenumber and get out to apply formatting
if (focusflag != true && formatflag !=true) {
validateNumber()
return }


//prevent putting in too many places after the decimal
var decimalplace = event.srcElement.value.indexOf(".");
// alert("checkspecial " + numdecimals)

if (decimalplace == -1) {
oldValue = event.srcElement.value
return;}


var theLen=event.srcElement.value.length;

var theDiff = theLen-decimalplace-1;

if (theDiff > numdecimals) {
//alert("You have entered too many places after the decimal - only " + numdecimals + " are allowed!")
formatflag = true;
event.srcElement.value = oldValue
}

oldValue = event.srcElement.value




}
function validateNumber() {

var tempstring



formatflag = true;

tempstring = event.srcElement.value;
if (tempstring.length == 0) return


if (tempstring.indexOf(".") == -1)
tempstring = tempstring + ".00"

var decimalplace = tempstring.indexOf(".",0)
var theLen=tempstring.length;

var theDiff = theLen-decimalplace-1;

if (theDiff==1) tempstring = tempstring + "0"
if (theDiff==0) tempstring = tempstring + "00"


//don't need comma's inserted if len < 4
if (tempstring.length < 7) {

if (tempstring.substring(0,1) != "$") {
event.srcElement.value = "$" + tempstring }

event.returnValue=true;
return true;
}
var decimalplace = tempstring.indexOf(".",0)

var slen = tempstring.length;

var tempvalue = ''
var intloop = 13;

var charcount=slen - (slen - decimalplace)
var numcount=0;
var tempchar;
tempvalue = tempstring.substring(decimalplace,slen)

//for currency number ignore after decimal and $
do {
numcount = numcount + 1;
charcount = charcount - 1;
tempchar = tempstring.substring(charcount,charcount+1)

tempvalue = tempchar + tempvalue
if (numcount==3 && charcount > 0 ) {
tempvalue = "," + tempvalue
numcount = 0
}


}
while (charcount > 0);



if (event.srcElement.value.substring(0,1) != "$")
tempvalue = "$" + tempvalue

event.srcElement.value = tempvalue;


formatflag = false
focusflag = false
event.returnValue=true;
return true;


}

function validateInput() {



var objSelRange = event.srcElement.document.selection.createRange();
var selectedText = objSelRange.text;
AllSelected = false;
if (selectedText == event.srcElement.value) AllSelected = true;


if (AllSelected==true && event.keyCode==46) {
event.returnValue=true;
return true;}

if (AllSelected==true && event.keyCode==32) {
event.returnValue = true;
lastkeydelete = true;
event.srcElement.value = ""
return true;}


if (event.shiftKey && event.keyCode==36) {
event.returnValue=true;
return true;}

if (event.shiftKey && event.keyCode==9) {
event.returnValue=true;
return true;}


if (event.shiftKey && event.keyCode==35) {
event.returnValue=true;
return true;}


if (event.shiftKey) {
event.returnValue=false;
return false;}

var theKey = event.keyCode;
var newkey = 0;

// alert(theKey)
//handle numeric keypad info by converting to numeric code from top of keyboard
if (theKey > 95 && theKey < 106) {
newkey = theKey - 48;
event.keyCode=newkey;
theKey=newkey;

}

if (theKey==9||theKey==35||theKey==36||theKey==39||theKey==46){ // let home,Tab, End, right arrow, delete keys go through
return true;
}

// convert left arrow to backspace - go through
if (theKey==37||theKey==8){
event.keyCode=(0x08);
return true;
}

if (event.ctrlKey && theKey==86) {
pasteNumber();
event.returnValue=false;
return false; }

if (event.ctrlKey && theKey==67) {
copyNumber();
event.returnValue=false;
return false; }


var pos1 = event.srcElement.value.indexOf(".",0)

var decimalpressed = false;

if (theKey == 110 || theKey==190) decimalpressed = true
if (theKey == 189) theKey = 109;

// only allow 1 decimal to be entered or none if numdecimals = 0
if ((pos1 > -1 || numdecimals==0) && decimalpressed==true) {
event.returnValue=false;
return;}

// if (event.srcElement.allownegative != true && event.srcElement.value.length==0 && theKey==109)
// {
// event.returnValue=true;
// return;
// }



if ((theKey < 48 || theKey > 57) && decimalpressed == false) {
event.returnValue=false;
return;}




}
function copyNumber()

{
window.clipboardData.SetData("text",event.srcElement.value)

}

function pasteNumber()
{

var reNumber = /^\$?\d+.?\d+$/;
var theString = window.clipboardData.getData('Text');



if (reNumber.test(theString) == false) {
alert("You are trying to paste an invalid Number!")
return; }

event.srcElement.value = theString
return;


}

</SCRIPT>

<script language="vbscript">

function checkvalidnumber(newvalue)

checkvalidnumber = true
if len(newvalue) = 0 then checkvalidnumber = false
if left(trim(newvalue),1) > "9" then checkvalidnumber = false
if left(trim(newvalue),1) = "-" then checkvalidnumber = false
if instr(newvalue,",") > 0 then checkvalidnumber = false
end function

</script>

<script language = "vbscript">

function replacecommas(origstring)

replacecommas = replace(origstring,",","")

end function

</script>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top