1

i have this jquery function

$('#contact_name').on('input', function() {
var input=$(this);
var re =/^[A-Za-z]+$/;
var is_email=re.test(input.val());
if(is_email)
{
}
else
{
}
});

for this text field

<label for="contact_name">Name:</label>
<input type="text" id="contact_Name" name="name"></input>

what i want is when the user types a number not letter i don't want the number to be displayed on the text field .. the text field only take letters and allow letters to be displayed on it and if it's a letter then display it .. so the user can know that this text field doesn't take numbers

how to do it ??

Lamawy
  • 603
  • 1
  • 7
  • 12
  • 1
    Perhaps this: http://stackoverflow.com/questions/19508183/how-to-force-input-to-only-allow-alpha-letters .. And... #contact_email magically appeared. Now, the question is: are you trying to check if the string is a valid e-mail or what? lol. Also, you want to AVOID the user to type numbers or do you want to allow him to and, then, remove them? I'm not getting exactly what you're looking for :) – briosheje Jun 18 '14 at 09:54
  • 1
    Where is `#contact_email`? – Shaunak D Jun 18 '14 at 09:55
  • "*the text field only take letters and allow letters to be displayed on it and if it's a letter then display it*" - that means allow both letters and numbers right..? – T J Jun 18 '14 at 09:56
  • @TJ no it allows only letters if its a number then it wont be displayed on the text field – Lamawy Jun 18 '14 at 09:57
  • @shaunakde sorry.. i edited the code – Lamawy Jun 18 '14 at 09:57
  • @user3542555 that's not what you have written in the description... – T J Jun 18 '14 at 09:57
  • http://stackoverflow.com/questions/19508183/how-to-force-input-to-only-allow-alpha-letters – Javascript Coder Jun 18 '14 at 10:00
  • Do you really need to restrict a contact name to `[A-Za-z]` range? http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – pawel Jun 18 '14 at 10:01
  • @briosheje i will check the link now .. and no its not an email lol for example a name if the user entered 5 i don't want it to be displayed on the text field only letters that will be displayed .. did i explain it better now ? – Lamawy Jun 18 '14 at 10:01
  • PLease create a js fiddle.. you are very poor with your question... What you want to achieve is not clear ? – Javascript Coder Jun 18 '14 at 10:01
  • Check this.. http://jsfiddle.net/y9Y4Y/1/ Your code is working.. want wrong with that.. what you want.. LOL – Javascript Coder Jun 18 '14 at 10:02
  • @CrazyAboutJavascript what i want is if i entered number 4 for example it will not be displayed on the text fields it will remains empty unless i entered a letter .. what will i put in the if else condition so it will work as i described.. idk is it clear now ? – Lamawy Jun 18 '14 at 10:06

3 Answers3

3

You may try this example

html

<input type="text" placeholder="Only letters" id="contact_name"/>

script

$(function()
{
    $("#contact_name").on('input', function()
    {
        this.value=this.value.replace(/[^a-zA-Z]/g,'');
    });
});
  • then go for the onpaste handler as well – Alexander Kludt Jun 18 '14 at 10:06
  • "*i don't want the number to be displayed on the text field*" - the numbers are being displayed before it's cleared... – T J Jun 18 '14 at 10:09
  • @Arvind i have a question this code is in javascript not jquery is it possible to do it with jquery with the code i posted above ? and what if i did it with asc code so if it this asc code (number) then never display it otherwise display the letters.. can you help me please ? – Lamawy Jun 18 '14 at 11:11
  • @Arvind yes this is what i want !! thank you so much !!! im really sorry i know i bothered you but i have a small question what if i want to enter a letter which is not English letter.. how to do it ? – Lamawy Jun 18 '14 at 11:45
0

use this function

 function chkLetter(event) {
      var v= event.keyCode;
      return ((v>= 65 && v<= 90) || v== 8);
    };
Amit Kumar
  • 5,888
  • 11
  • 47
  • 85
  • you could still paste text from clipboard – pawel Jun 18 '14 at 09:58
  • 1
    actually he told that "when the user types a number not letter i don't want the number to be displayed on the text field .. the text field only take letters and allow letters to be displayed on it and if it's a letter then display it " he didn't say about paste or anything. – Amit Kumar Jun 18 '14 at 10:01
0

you can try this way

<script type="text/javascript">

 function CheckValue(e){
 e=(window.event) ? event : e;

 return (/[A-Za-z]/.test(String.fromCharCode(e.keyCode))); 
 }   
</script>


<input type="text" onkeydown="return CheckValue(event)">
user3541964
  • 107
  • 2
  • 13
  • @TJ: That's not criticizing, that's pointing something out. Anyway this can be solved in this way http://stackoverflow.com/questions/1226574/disable-copy-paste-into-html-form-using-javascript :) – briosheje Jun 18 '14 at 10:06