homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

Check if user entered a numeric character

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

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;

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.



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


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.


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

You could easily achieve this using client side vbscript, which already has an IsNumeric function for you:
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.


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

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;



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

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?


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

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


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

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 '?

Global Options:
 top home search open messages active posts  

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved