Welcome to WebmasterWorld Guest from 107.20.122.81

Forum Moderators: ocean10000

Message Too Old, No Replies

Check if user entered a numeric character

     

pedrodepacos

2:31 am on Jul 29, 2003 (gmt 0)

10+ Year Member



I've written a client-side validation function (for a textbox) that checks if the key pressed by the user is numeric. I cannot get it to work properly, however. The following code does not prevent the user from entering a non-numeric character, but it does prevent the user from entering two non-numeric characters. For example if the user enters a "1" and the a "d", the textbox will display "1d". The next time the user presses a key, the "d" is removed and whatever character the user pressed replaces the "d". I don't want the textbox to display the character unless it is numeric.

'The following is entered in the codebehind

 
Dim txtQuantity As TextBox = e.Item.Cells(4).FindControl("txtQuantity")
txtQuantity.Attributes.Add("onkeypress", "IsNumeric(" & txtQuantity.ClientID & ",'Integer')")

'this is the javascript function called when the textbox's onkeypress event is fired

 
function IsNumeric(TextBox,strType)
// check for valid numeric strings
{
if (strType="Decimal")
{
var strValidChars = "0123456789.";
}
else if (strType="Integer")
{
var strValidChars = "0123456789";
}
var strString;
var strChar;
strString=TextBox.value;

for (i = 0; i < strString.length; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
TextBox.value = TextBox.value.substring(0, i);
TextBox.value.length = i
event.returnValue = false;
}
}
}

I'm not very proficient in Javascript so I'd appreciate any suggestions.

gangstah

3:44 pm on Jul 29, 2003 (gmt 0)

10+ Year Member



Hi,

You could do something like

function isNumber(text){

 var goodChars = "0123456789.";
[/code]
 var isNum = true;

 var char;

// loop through the chars in the text you passed in

for (var idx = 0; text.length; idx++)

{

char = text.charAt(i);

// if the char isn't in the string of good 

// characters then exit returning false

if (goodChars.indexOf(char) == -1)

{

 return false;

}

}

// if you made it this far then your gold

return true;

}

}

Hope that helps.

smokin

4:08 pm on Jul 29, 2003 (gmt 0)

10+ Year Member



You could easily achieve this using client side vbscript, which already has an IsNumeric function for you:
e.g
MyVar = 53 ' Assign a value.
MyCheck = IsNumeric(MyVar) ' Returns True.
MyVar = "459.95" ' Assign a value.
MyCheck = IsNumeric(MyVar) ' Returns True.
MyVar = "45 Help" ' Assign a value.
MyCheck = IsNumeric(MyVar) ' Returns False.

garann

7:27 pm on Jul 29, 2003 (gmt 0)

10+ Year Member



Yet another approach (client-side Javascript):


// pass a reference to the field using:
// onkeypress/onchange/onblur='chkNumeric(this)'
function chkNumeric(obj) {
var val = "";
//split the string every time a non-numeric is found
var vals = obj.value.split(/(\D)/);
for (var i=0;i<vals.length;i++)
//make your leftover characters, all numeric, into a string
val += vals[i];
obj.value = val;
...
}

hth,
g.

pedrodepacos

10:21 pm on Jul 29, 2003 (gmt 0)

10+ Year Member



Thank you for all you suggestions, and I have now gotten it to work properly. I just have one more, slightly unrelated, question. Are there any characters that I need to replace, or exclude when adding a string (entered by the user) to a MS Access database?

smokin

11:56 pm on Jul 29, 2003 (gmt 0)

10+ Year Member



The following characters need replacing when doing an insert into access db:
[ '

pedrodepacos

1:47 pm on Jul 30, 2003 (gmt 0)

10+ Year Member



Thanks smokin, but what do I replace them with. I think I remember reading somewhere that you should replace ' with two apostrophes ''. Do I replace [ with '[?

When I get the string from the database, do I have to then replace the '' with a single '?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month