Enter Button express as regular expression

Hi,

I am trying to add the Enter button to the following condition:

allow=“/[A-Za-z\s,.?!]/”

Does anyone know how to achieve it?

DH

Just pushing this thread up, hope someone can post a solution.

thanks,

DH

There is no way to match an enter with a regular expression, since regular expressions are made to match string patterns, not keyboard input. (I know that is what you use it for, but still).

Your best bet would be to check the keycode of the special keys, before you check against the regular expression. The enter key is most likely keycode 13 (I havent checked, but you can easily check by printing the code to the screen).

Hi TSS,

could you please clarify what you mean by

… Your best bet would be to check the keycode of the special keys, before you check against the regular expression.

and by

… printing the code to the screen.

DH

Well, look at the following line out the code you are using:


   if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;

It uses the event.keyCode, which ofcourse you can check or print directly as well. For example, you could insert an alert in front of the if statement reading:


alert ("Code for this key is: " + event.keyCode);

Once you know the code for the enter button (probably 13, but you can check with the above function), you can then put that in an if statement as well. Possible constructs would be to first check if the keyCode = 13, if it is, just let it pass through, and else, check with the regular expression.

Ofcourse you can make both expressions into one IF-statement as well, it’s all about what you want to do with it.

It’s true that there will be no ‘enter key character’ in the string from a textbox and you will need to resort to event processing, but if you are capturing text from a textarea, a newline character (
) can be detected in the string.

This is a followup on one of his earlier posts in this javascript section. He wanted to allow certain keys to be pressed (and others get their event cancelled) by checking against a regular expression. Take a look at:
http://www.sitepointforums.com/showthread.php?t=142118

Thanks TSS,

quite interesting, didn’t expect you to be around, hence the new post. It is 13 as you suggested. So do I have to do the following now?:


if (!String.fromCharCode(13)) event.returnValue=true;
   if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;

It seems to work, is it the correct way though?

DH

EDIT: nope, gives errors, should I be using an elseif?

Sure as hell gives errors :slight_smile:

I would think something along the lines of the following should work:


if (event.keyCode == 13) {
	// Do something
	alert ("You pressed the enter key, naughty boy");
} else {
	// Check for valid input, if invalid, cancel this event.
	if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;
}

HI TSS,

thanks but I want the user to be able to press the enter button as a general rule. I have tried the following:


function filterInput(e) {
   // Get the regular expression to test against for this particular object
   regAllow = (e)?eval(e.allow):eval(event.srcElement.allow);
   if (event.keyCode == 13) {
	// Do something
	event.returnValue=true;
	} else {
	// Check for valid input, if invalid, cancel this event.
	if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;
	}

but now it allows all characters…

why?

DH

This works fine for me:


<script>
function filterInput(e) {
// Get the regular expression to test against for this particular object
regAllow = (e)?eval(e.allow):eval(event.srcElement.allow);
// Check for an allowed character, if not found, cancel the keypress's event bubbling
if (event.keyCode == 13) {
	// Do nothing, i.e. allow.
} else {
	if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;
}
}
</script>
 

You never have to set the events returnvalue to true, since it bubbles up by default anyways. You only have to cancel when you recieve input you dont want.

Gotcha, thanks TSS, going to study some more javascript I think.

DH