How to check a numeric input field for alphabetic characters

How to check a numeric input field for alphabetic characters

For a numeric field that's on a screen for input, how do I check to see if some has entered alphabetic characters (like 'abc')?
Hello Gerry,

You can use the "onkeypress" event in the extended property and in the value put the following javascript code:

"var e = (arguments[0] || window.event); if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) { return false; }"

Best Regards,
Gonçalo Martins
- gerry
While this works in IE9 and FireFox, we have users on IE7 and IE8 (yes, :( ) any ideas what could be causing this?
Hi Tim,

Not sure if it will work in all browsers, but feel free to try this.

First add an extended property to your input:
  • Name: onkeypress
  • Value: "Edit_ValidateNumber(event);"
Then add this code to your screen / web block javascript:

/* Only digits input */
function Edit_ValidateNumber(event)
    if(!CheckInputSubmit(event)) {  // Discard Enter key
        return false;
    var key = event.keyCode || theEvent.which;
    // Allow: backspace, delete, tab, escape, enter and .
    if ( $.inArray(key,[46,8,9,27,13,190]) !== -1 ||
         // Allow: Ctrl+A
        (key == 65 && event.ctrlKey === true) ||
         // Allow: home, end, left, right
        (key >= 35 && event.keyCode <= 39)) {
             // let it happen, don't do anything
    else {
        // Ensure that it is a number and stop the keypress
        if (event.shiftKey || ((key < 48 || key > 57) && (key != 46))) {

/* Avoid submitting the page when clicking Enter */
function CheckInputSubmit(e) {
    e = e || window.event;
    var key = e.keyCode || e.charCode;
    if(key == 13) e.preventDefault();
    return key !== 13;

  • The code above also prevents the user to submit the page by clicking Enter. Drop the CheckInputSubmit() references to maintain the default behavior.
  • The code above allows decimals, you will have to tweak it slightly if you just want to validate integers.

Why not just use the "TextToDecimalValidate()" action in the beginning of your flow? This way you don't divide your code among Outsystems and Javascript and its easier for someone else to understand in the future.

P.S: This is in no way an anti-javascript post because I do use it a lot! Just not in these cases... ;)

Hermínio Mira
Different problems, Hermínio. He wants to avoid having the user making mistakes, instead of validating and reporting those mistakes after they happen. It improves the user experience, imho.
Regarding code maintenance, this kind of stuff can be encapsulated in a web block, for example, so that your development team can easily reuse it - in the same way they are already using RichWidgets.
Thank you for that script, Paulo, but in IE7, the "event.preventDefault();" doesn't exist, and throws an error. 

--> fixed that by replacing it with: 
event.preventDefault ? event.preventDefault() : event.returnValue = false;
Yeah, event.preventDefault is not available in IE7... thanks for the workaround.