Question

I'm doing, what everybody does on the forums, but something goes wrong, I can't validate my forms on mozilla firefox with my initial code, it runs well on any browser but not on mozilla, the initial code was:

function ValidateOnlyText() {
 if ((event.keyCode != 32) && (event.keyCode < 65) 
    || (event.keyCode > 90) && (event.keyCode < 97) 
    || (event.keyCode > 122))
    {alert("only text");
  event.returnValue = false;
}

That function is executed, everytime the user writes something on an input with, onkeypress, an example:

<input class="inputShort" name="lastname" type="text" id="lastname" onkeypress="ValidateOnlyText()"/>

So, searching and searching, I found it a lot of people that corrects that issue on firefox, and I test it, keyCode for the other browsers, and which for firefox, I do that on the javascript code:

function ValidaSoloTextos(e) {
    var key=document.all?e.keyCode:e.which;
    if ((key!=32)&&(key<65)||(key>90)&&(key<97)||(key>122)){
        alert("solo texto");
        return false;
    }
}

and add on the input this:

onkeypress="ValidaSoloTextos(this.value)"

What is wrong on that? I'm going to search more, and if I find something I update the post, thanks all

Was it helpful?

Solution

You need to pass an event to your function:

<input class="inputShort" name="lastname" type="text" id="lastname" onkeypress="ValidateOnlyText(event)"/>

So change this.value to event

Also I would change document.all to e.keyCode, makes more sense.

Add return before ValidateOnlyText(event) to stop unwanted characters from being typed in.

This might also be of any use to you:
Allow only numbers to be typed in a textbox

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top