3

I have a text field and I want to allow only alphabet. So I write the below Javascript method to prevent other keys. Unfortunately my text field is allowing uparrow(^). Can any one tell me how to restrict uparrow(^) ?

function onlyAlphabet(evt) {
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;

    var keychar = String.fromCharCode(key);
    var keycheck = /[a-zA-z\s]/;

    if (!(key == 8 || key == 27 || key == 46 || key == 9 || key == 39)) // backspace delete  escape arrows
    {
        if (!keycheck.test(keychar)) {
            theEvent.returnValue = false;//for IE
            if (theEvent.preventDefault)
                theEvent.preventDefault();//Firefox
        }
    }
}

html code

<div class="form-group">
   <label class="form-text">Travels Name</label>
   <h:inputText value="#{bean.travelName}" maxlength="50" onkeypress="return onlyAlphabet(event)" />
</div>
Mamun
  • 66,969
  • 9
  • 47
  • 59
Mihir
  • 75
  • 1
  • 9

6 Answers6

4

You can achieve that in a more simpler way like the following:

function onlyAlphabet(inputVal) {
  var patt=/^[a-zA-Z]+$/;
  if(patt.test(inputVal)){
    document.getElementById('txtTravel').value = inputVal;
  }
  else{
    var txt = inputVal.slice(0, -1);
    document.getElementById('txtTravel').value = txt;
  }
  
}
<div class="form-group">
    <label class="form-text">Travels Name</label>
    <input id="txtTravel" type="text" maxlength="50"
     oninput="onlyAlphabet(value)" />
 </div>
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

<!DOCTYPE html>
<html>
<head>
    <title></title>
 <meta charset="utf-8" />
    <script>
        function onlyAlphabet(evt) {
            var theEvent = evt || window.event;
            var key = theEvent.keyCode || theEvent.which;

            var keychar = String.fromCharCode(key);
            var keycheck = /[a-zA-z\s]/;

            if (!(key == 8 || key == 27 || key == 46 || key == 9 || key == 39 || key == 94)) // backspace delete  escape arrows
            {
               
                    if (!keycheck.test(keychar)) {
                        theEvent.returnValue = false;//for IE
                        if (theEvent.preventDefault)
                            theEvent.preventDefault();//Firefox
                    }
                
                
            }
            else if(key==94)
                theEvent.preventDefault();
        }
    </script>
</head>
<body>

   
    <input id="Text1" type="text"  onkeypress="return onlyAlphabet(event)"/>
</body>
</html>

Keycode for ^ is 94 .. Prevent it.. If you want to fix it yourself put alert(key) in the code, where you will get the code for the pressed key.

Ratheesh
  • 549
  • 4
  • 15
0

The most elegant way (not supported in Safari) is to use the pattern attribute of the input element.

<input pattern="[A-Za-z\s\^]+" type="text" />

Read more here

For the `javascript` approach, codes for arrow keys are: { left_arrow : 37, up_arrow : 38, right_arrow : 39, down_arrow : 40 } In your if condition you have to eliminate 38 and 40.

Oh, you mean the ^ itself? :)))

It is enough to update your regular expression:

var keycheck = /[a-zA-z\s\^]/;
bluehipy
  • 2,254
  • 1
  • 13
  • 19
0

Using jQuery, simple & elegant :

jQuery('#id').on('keypress', limitChars);

function limitChars(e){
     return event.charCode >= 97 && event.charCode <= 122;
}
John Libes
  • 363
  • 3
  • 11
0

You should try to block only the specific key you don't want. So keypress event get the pressed key before this character enter in your input. You also can use event.preventDefault() to block this specific key you don't want.

I'll show you a good example of how this could be done using jQuery.

$("input[name='yourinput']").keypress(function(event) {
    if ( event.keyCode == 94 ) {
        event.preventDefault();
    }
});
Rafael Xavier
  • 956
  • 13
  • 13
0

This also works

<div class="form-group">
   <label class="form-text">Travels Name</label>
   <h:inputText value="#{bean.travelName}" maxlength="50" onkeypress="/[a-zA-Z]/.test(event.key) || event.preventDefault()" />
</div>
eOssa
  • 44
  • 5