| Check if user entered a numeric character
|
pedrodepacos

msg:953991 | 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; 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

msg:953992 | 3:44 pm on Jul 29, 2003 (gmt 0) | 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

msg:953993 | 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: 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

msg:953994 | 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; ... }
hth, g.
|
pedrodepacos

msg:953995 | 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?
|
smokin

msg:953996 | 11:56 pm on Jul 29, 2003 (gmt 0) | The following characters need replacing when doing an insert into access db: [ '
|
pedrodepacos

msg:953997 | 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 '?
|
|
|