1

I added validateConstraint module to my edit box and it works fine when you submit the form:

<xp:validateConstraint
message="Only letters, numbers and the space character allowed in this field">
<xp:this.regex><![CDATA[^[a-zA-Z0-9_ ]*$]]></xp:this.regex>
</xp:validateConstraint>

But how do I prevent entering/paste such characters. Assume I need to put some replace statement into keyDown event but cannot figure out what exactly.

VladP
  • 881
  • 2
  • 13
  • 37

1 Answers1

2

You can use the onkeypress event to limit just the characters you want to accept.

This doesn't validate the special characters, it doesn't allow the user to type them. Here is a site that will tell you the keycode: http://keycode.info/

For example, this will just allow numbers:

var keyCode = event.keyCode;
if((keyCode >= 48 && keyCode <= 57) ||keyCode == 8){
    event.returnValue = true;
}else{
    event.returnValue = false;
}

You could also reverse this and selectively exclude special characters by their keycode.

Here is a picture of how I do it. I don't use JQuery like the link in the comments, although that certainly works too: enter image description here

One caveat: For use in a Edit box (in XPages), the keypad characters work the same even though they have different keycodes. In some cases, like a Dojo Comboxbox, you need to also specify both the normal numbers, and the keypad numbers. keyCode values for numeric keypad?

Community
  • 1
  • 1
Steve Zavocki
  • 1,840
  • 4
  • 21
  • 35
  • Here is a blog post I wrote on this subject: http://notesspeak.blogspot.com/2015/11/limiting-keyboard-input-in-xpages.html – Steve Zavocki Nov 17 '15 at 16:59