jQuery validation plugin: accept only alphabetical characters?











up vote
14
down vote

favorite
12












I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.



Any ideas?



Appreciate your help.










share|improve this question




























    up vote
    14
    down vote

    favorite
    12












    I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.



    Any ideas?



    Appreciate your help.










    share|improve this question


























      up vote
      14
      down vote

      favorite
      12









      up vote
      14
      down vote

      favorite
      12






      12





      I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.



      Any ideas?



      Appreciate your help.










      share|improve this question















      I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.



      Any ideas?



      Appreciate your help.







      jquery validation jquery-validate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 21 '10 at 22:45









      Pavel Chuchuva

      18.6k67893




      18.6k67893










      asked May 8 '10 at 13:05









      KeyStroke

      77231524




      77231524
























          6 Answers
          6






          active

          oldest

          votes

















          up vote
          75
          down vote



          accepted










          If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js



          You can use the lettersonly rule :) The additional methods are part of the zip you download, you can always find the latest here.



          Here's an example:



          $("form").validate({
          rules: {
          myField: { lettersonly: true }
          }
          });


          It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate() call:



          jQuery.validator.addMethod("lettersonly", function(value, element) {
          return this.optional(element) || /^[a-z]+$/i.test(value);
          }, "Letters only please");





          share|improve this answer























          • ^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
            – c-chavez
            Sep 24 '17 at 13:45












          • upvoted for this.optional(element) ||
            – Pankaj
            Jan 19 at 20:01


















          up vote
          14
          down vote













          Be careful,



          jQuery.validator.addMethod("lettersonly", function(value, element) 
          {
          return this.optional(element) || /^[a-z," "]+$/i.test(value);
          }, "Letters and spaces only please");


          [a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.



          For spaces + text, just do this:



          jQuery.validator.addMethod("lettersonly", function(value, element) 
          {
          return this.optional(element) || /^[a-z ]+$/i.test(value);
          }, "Letters and spaces only please");


          [a-z ] this allows spaces aswell as text only.



          ............................................................................



          also the message "Letters and spaces only please" is not required, if you already have a message in messages:



          messages:{
          firstname:{
          required: "Enter your first name",
          minlength: jQuery.format("Enter at least (2) characters"),
          maxlength:jQuery.format("First name too long more than (80) characters"),
          lettersonly:jQuery.format("letters only mate")
          },


          Adam






          share|improve this answer




























            up vote
            4
            down vote













            Just a small addition to Nick's answer (which works great !) :



            If you want to allow spaces in between letters, for example ,
            you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:



            jQuery.validator.addMethod("lettersonly", function(value, element) 
            {
            return this.optional(element) || /^[a-z," "]+$/i.test(value);
            }, "Letters and spaces only please");





            share|improve this answer






























              up vote
              2
              down vote













              Both Nick and Adam's answers work really well.



              I'd like to add a note if you want to allow latin characters like á and ç as I wanted to do:



              jQuery.validator.addMethod('lettersonly', function(value, element) {
              return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
              }, "Letters and spaces only please");





              share|improve this answer



















              • 1




                Thanks! but u also forgot ñ hehe :)
                – Andres
                Nov 22 '16 at 15:20










              • Thanks, @Andres! I just updated my answer and added ñ
                – FlavioEscobar
                Nov 23 '16 at 17:53


















              up vote
              1
              down vote













              to allow letters ans spaces



              jQuery.validator.addMethod("lettersonly", function(value, element) {
              return this.optional(element) || /^[a-zs]+$/i.test(value);
              }, "Only alphabetical characters");

              $('#yourform').validate({
              rules: {
              name_field: {
              lettersonly: true
              }
              }
              });





              share|improve this answer




























                up vote
                0
                down vote













                 $('.AlphabetsOnly').keypress(function (e) {
                var regex = new RegExp(/^[a-zA-Zs]+$/);
                var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
                if (regex.test(str)) {
                return true;
                }
                else {
                e.preventDefault();
                return false;
                }
                });





                share|improve this answer





















                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2794162%2fjquery-validation-plugin-accept-only-alphabetical-characters%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  75
                  down vote



                  accepted










                  If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js



                  You can use the lettersonly rule :) The additional methods are part of the zip you download, you can always find the latest here.



                  Here's an example:



                  $("form").validate({
                  rules: {
                  myField: { lettersonly: true }
                  }
                  });


                  It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate() call:



                  jQuery.validator.addMethod("lettersonly", function(value, element) {
                  return this.optional(element) || /^[a-z]+$/i.test(value);
                  }, "Letters only please");





                  share|improve this answer























                  • ^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
                    – c-chavez
                    Sep 24 '17 at 13:45












                  • upvoted for this.optional(element) ||
                    – Pankaj
                    Jan 19 at 20:01















                  up vote
                  75
                  down vote



                  accepted










                  If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js



                  You can use the lettersonly rule :) The additional methods are part of the zip you download, you can always find the latest here.



                  Here's an example:



                  $("form").validate({
                  rules: {
                  myField: { lettersonly: true }
                  }
                  });


                  It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate() call:



                  jQuery.validator.addMethod("lettersonly", function(value, element) {
                  return this.optional(element) || /^[a-z]+$/i.test(value);
                  }, "Letters only please");





                  share|improve this answer























                  • ^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
                    – c-chavez
                    Sep 24 '17 at 13:45












                  • upvoted for this.optional(element) ||
                    – Pankaj
                    Jan 19 at 20:01













                  up vote
                  75
                  down vote



                  accepted







                  up vote
                  75
                  down vote



                  accepted






                  If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js



                  You can use the lettersonly rule :) The additional methods are part of the zip you download, you can always find the latest here.



                  Here's an example:



                  $("form").validate({
                  rules: {
                  myField: { lettersonly: true }
                  }
                  });


                  It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate() call:



                  jQuery.validator.addMethod("lettersonly", function(value, element) {
                  return this.optional(element) || /^[a-z]+$/i.test(value);
                  }, "Letters only please");





                  share|improve this answer














                  If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js



                  You can use the lettersonly rule :) The additional methods are part of the zip you download, you can always find the latest here.



                  Here's an example:



                  $("form").validate({
                  rules: {
                  myField: { lettersonly: true }
                  }
                  });


                  It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate() call:



                  jQuery.validator.addMethod("lettersonly", function(value, element) {
                  return this.optional(element) || /^[a-z]+$/i.test(value);
                  }, "Letters only please");






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 8 '10 at 13:20

























                  answered May 8 '10 at 13:10









                  Nick Craver

                  527k11511891097




                  527k11511891097












                  • ^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
                    – c-chavez
                    Sep 24 '17 at 13:45












                  • upvoted for this.optional(element) ||
                    – Pankaj
                    Jan 19 at 20:01


















                  • ^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
                    – c-chavez
                    Sep 24 '17 at 13:45












                  • upvoted for this.optional(element) ||
                    – Pankaj
                    Jan 19 at 20:01
















                  ^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
                  – c-chavez
                  Sep 24 '17 at 13:45






                  ^[A-Za-z]+$ to also accept capital letters, or set value to lower case with toLowerCase() function.
                  – c-chavez
                  Sep 24 '17 at 13:45














                  upvoted for this.optional(element) ||
                  – Pankaj
                  Jan 19 at 20:01




                  upvoted for this.optional(element) ||
                  – Pankaj
                  Jan 19 at 20:01












                  up vote
                  14
                  down vote













                  Be careful,



                  jQuery.validator.addMethod("lettersonly", function(value, element) 
                  {
                  return this.optional(element) || /^[a-z," "]+$/i.test(value);
                  }, "Letters and spaces only please");


                  [a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.



                  For spaces + text, just do this:



                  jQuery.validator.addMethod("lettersonly", function(value, element) 
                  {
                  return this.optional(element) || /^[a-z ]+$/i.test(value);
                  }, "Letters and spaces only please");


                  [a-z ] this allows spaces aswell as text only.



                  ............................................................................



                  also the message "Letters and spaces only please" is not required, if you already have a message in messages:



                  messages:{
                  firstname:{
                  required: "Enter your first name",
                  minlength: jQuery.format("Enter at least (2) characters"),
                  maxlength:jQuery.format("First name too long more than (80) characters"),
                  lettersonly:jQuery.format("letters only mate")
                  },


                  Adam






                  share|improve this answer

























                    up vote
                    14
                    down vote













                    Be careful,



                    jQuery.validator.addMethod("lettersonly", function(value, element) 
                    {
                    return this.optional(element) || /^[a-z," "]+$/i.test(value);
                    }, "Letters and spaces only please");


                    [a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.



                    For spaces + text, just do this:



                    jQuery.validator.addMethod("lettersonly", function(value, element) 
                    {
                    return this.optional(element) || /^[a-z ]+$/i.test(value);
                    }, "Letters and spaces only please");


                    [a-z ] this allows spaces aswell as text only.



                    ............................................................................



                    also the message "Letters and spaces only please" is not required, if you already have a message in messages:



                    messages:{
                    firstname:{
                    required: "Enter your first name",
                    minlength: jQuery.format("Enter at least (2) characters"),
                    maxlength:jQuery.format("First name too long more than (80) characters"),
                    lettersonly:jQuery.format("letters only mate")
                    },


                    Adam






                    share|improve this answer























                      up vote
                      14
                      down vote










                      up vote
                      14
                      down vote









                      Be careful,



                      jQuery.validator.addMethod("lettersonly", function(value, element) 
                      {
                      return this.optional(element) || /^[a-z," "]+$/i.test(value);
                      }, "Letters and spaces only please");


                      [a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.



                      For spaces + text, just do this:



                      jQuery.validator.addMethod("lettersonly", function(value, element) 
                      {
                      return this.optional(element) || /^[a-z ]+$/i.test(value);
                      }, "Letters and spaces only please");


                      [a-z ] this allows spaces aswell as text only.



                      ............................................................................



                      also the message "Letters and spaces only please" is not required, if you already have a message in messages:



                      messages:{
                      firstname:{
                      required: "Enter your first name",
                      minlength: jQuery.format("Enter at least (2) characters"),
                      maxlength:jQuery.format("First name too long more than (80) characters"),
                      lettersonly:jQuery.format("letters only mate")
                      },


                      Adam






                      share|improve this answer












                      Be careful,



                      jQuery.validator.addMethod("lettersonly", function(value, element) 
                      {
                      return this.optional(element) || /^[a-z," "]+$/i.test(value);
                      }, "Letters and spaces only please");


                      [a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.



                      For spaces + text, just do this:



                      jQuery.validator.addMethod("lettersonly", function(value, element) 
                      {
                      return this.optional(element) || /^[a-z ]+$/i.test(value);
                      }, "Letters and spaces only please");


                      [a-z ] this allows spaces aswell as text only.



                      ............................................................................



                      also the message "Letters and spaces only please" is not required, if you already have a message in messages:



                      messages:{
                      firstname:{
                      required: "Enter your first name",
                      minlength: jQuery.format("Enter at least (2) characters"),
                      maxlength:jQuery.format("First name too long more than (80) characters"),
                      lettersonly:jQuery.format("letters only mate")
                      },


                      Adam







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 24 '15 at 3:48









                      adam

                      14112




                      14112






















                          up vote
                          4
                          down vote













                          Just a small addition to Nick's answer (which works great !) :



                          If you want to allow spaces in between letters, for example ,
                          you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:



                          jQuery.validator.addMethod("lettersonly", function(value, element) 
                          {
                          return this.optional(element) || /^[a-z," "]+$/i.test(value);
                          }, "Letters and spaces only please");





                          share|improve this answer



























                            up vote
                            4
                            down vote













                            Just a small addition to Nick's answer (which works great !) :



                            If you want to allow spaces in between letters, for example ,
                            you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:



                            jQuery.validator.addMethod("lettersonly", function(value, element) 
                            {
                            return this.optional(element) || /^[a-z," "]+$/i.test(value);
                            }, "Letters and spaces only please");





                            share|improve this answer

























                              up vote
                              4
                              down vote










                              up vote
                              4
                              down vote









                              Just a small addition to Nick's answer (which works great !) :



                              If you want to allow spaces in between letters, for example ,
                              you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:



                              jQuery.validator.addMethod("lettersonly", function(value, element) 
                              {
                              return this.optional(element) || /^[a-z," "]+$/i.test(value);
                              }, "Letters and spaces only please");





                              share|improve this answer














                              Just a small addition to Nick's answer (which works great !) :



                              If you want to allow spaces in between letters, for example ,
                              you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:



                              jQuery.validator.addMethod("lettersonly", function(value, element) 
                              {
                              return this.optional(element) || /^[a-z," "]+$/i.test(value);
                              }, "Letters and spaces only please");






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 13 '14 at 14:21









                              Bruno Lange

                              630710




                              630710










                              answered Dec 29 '13 at 20:53









                              Steer360

                              4122613




                              4122613






















                                  up vote
                                  2
                                  down vote













                                  Both Nick and Adam's answers work really well.



                                  I'd like to add a note if you want to allow latin characters like á and ç as I wanted to do:



                                  jQuery.validator.addMethod('lettersonly', function(value, element) {
                                  return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
                                  }, "Letters and spaces only please");





                                  share|improve this answer



















                                  • 1




                                    Thanks! but u also forgot ñ hehe :)
                                    – Andres
                                    Nov 22 '16 at 15:20










                                  • Thanks, @Andres! I just updated my answer and added ñ
                                    – FlavioEscobar
                                    Nov 23 '16 at 17:53















                                  up vote
                                  2
                                  down vote













                                  Both Nick and Adam's answers work really well.



                                  I'd like to add a note if you want to allow latin characters like á and ç as I wanted to do:



                                  jQuery.validator.addMethod('lettersonly', function(value, element) {
                                  return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
                                  }, "Letters and spaces only please");





                                  share|improve this answer



















                                  • 1




                                    Thanks! but u also forgot ñ hehe :)
                                    – Andres
                                    Nov 22 '16 at 15:20










                                  • Thanks, @Andres! I just updated my answer and added ñ
                                    – FlavioEscobar
                                    Nov 23 '16 at 17:53













                                  up vote
                                  2
                                  down vote










                                  up vote
                                  2
                                  down vote









                                  Both Nick and Adam's answers work really well.



                                  I'd like to add a note if you want to allow latin characters like á and ç as I wanted to do:



                                  jQuery.validator.addMethod('lettersonly', function(value, element) {
                                  return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
                                  }, "Letters and spaces only please");





                                  share|improve this answer














                                  Both Nick and Adam's answers work really well.



                                  I'd like to add a note if you want to allow latin characters like á and ç as I wanted to do:



                                  jQuery.validator.addMethod('lettersonly', function(value, element) {
                                  return this.optional(element) || /^[a-z áãâäàéêëèíîïìóõôöòúûüùçñ]+$/i.test(value);
                                  }, "Letters and spaces only please");






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 23 '16 at 17:52

























                                  answered Sep 8 '15 at 17:38









                                  FlavioEscobar

                                  460614




                                  460614








                                  • 1




                                    Thanks! but u also forgot ñ hehe :)
                                    – Andres
                                    Nov 22 '16 at 15:20










                                  • Thanks, @Andres! I just updated my answer and added ñ
                                    – FlavioEscobar
                                    Nov 23 '16 at 17:53














                                  • 1




                                    Thanks! but u also forgot ñ hehe :)
                                    – Andres
                                    Nov 22 '16 at 15:20










                                  • Thanks, @Andres! I just updated my answer and added ñ
                                    – FlavioEscobar
                                    Nov 23 '16 at 17:53








                                  1




                                  1




                                  Thanks! but u also forgot ñ hehe :)
                                  – Andres
                                  Nov 22 '16 at 15:20




                                  Thanks! but u also forgot ñ hehe :)
                                  – Andres
                                  Nov 22 '16 at 15:20












                                  Thanks, @Andres! I just updated my answer and added ñ
                                  – FlavioEscobar
                                  Nov 23 '16 at 17:53




                                  Thanks, @Andres! I just updated my answer and added ñ
                                  – FlavioEscobar
                                  Nov 23 '16 at 17:53










                                  up vote
                                  1
                                  down vote













                                  to allow letters ans spaces



                                  jQuery.validator.addMethod("lettersonly", function(value, element) {
                                  return this.optional(element) || /^[a-zs]+$/i.test(value);
                                  }, "Only alphabetical characters");

                                  $('#yourform').validate({
                                  rules: {
                                  name_field: {
                                  lettersonly: true
                                  }
                                  }
                                  });





                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote













                                    to allow letters ans spaces



                                    jQuery.validator.addMethod("lettersonly", function(value, element) {
                                    return this.optional(element) || /^[a-zs]+$/i.test(value);
                                    }, "Only alphabetical characters");

                                    $('#yourform').validate({
                                    rules: {
                                    name_field: {
                                    lettersonly: true
                                    }
                                    }
                                    });





                                    share|improve this answer























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      to allow letters ans spaces



                                      jQuery.validator.addMethod("lettersonly", function(value, element) {
                                      return this.optional(element) || /^[a-zs]+$/i.test(value);
                                      }, "Only alphabetical characters");

                                      $('#yourform').validate({
                                      rules: {
                                      name_field: {
                                      lettersonly: true
                                      }
                                      }
                                      });





                                      share|improve this answer












                                      to allow letters ans spaces



                                      jQuery.validator.addMethod("lettersonly", function(value, element) {
                                      return this.optional(element) || /^[a-zs]+$/i.test(value);
                                      }, "Only alphabetical characters");

                                      $('#yourform').validate({
                                      rules: {
                                      name_field: {
                                      lettersonly: true
                                      }
                                      }
                                      });






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 12 '16 at 11:13









                                      Sameera Prasad Jayasinghe

                                      455411




                                      455411






















                                          up vote
                                          0
                                          down vote













                                           $('.AlphabetsOnly').keypress(function (e) {
                                          var regex = new RegExp(/^[a-zA-Zs]+$/);
                                          var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
                                          if (regex.test(str)) {
                                          return true;
                                          }
                                          else {
                                          e.preventDefault();
                                          return false;
                                          }
                                          });





                                          share|improve this answer

























                                            up vote
                                            0
                                            down vote













                                             $('.AlphabetsOnly').keypress(function (e) {
                                            var regex = new RegExp(/^[a-zA-Zs]+$/);
                                            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
                                            if (regex.test(str)) {
                                            return true;
                                            }
                                            else {
                                            e.preventDefault();
                                            return false;
                                            }
                                            });





                                            share|improve this answer























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                               $('.AlphabetsOnly').keypress(function (e) {
                                              var regex = new RegExp(/^[a-zA-Zs]+$/);
                                              var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
                                              if (regex.test(str)) {
                                              return true;
                                              }
                                              else {
                                              e.preventDefault();
                                              return false;
                                              }
                                              });





                                              share|improve this answer












                                               $('.AlphabetsOnly').keypress(function (e) {
                                              var regex = new RegExp(/^[a-zA-Zs]+$/);
                                              var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
                                              if (regex.test(str)) {
                                              return true;
                                              }
                                              else {
                                              e.preventDefault();
                                              return false;
                                              }
                                              });






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered May 23 '17 at 12:35









                                              Anup Shetty

                                              30838




                                              30838






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2794162%2fjquery-validation-plugin-accept-only-alphabetical-characters%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Berounka

                                                  Sphinx de Gizeh

                                                  Different font size/position of beamer's navigation symbols template's content depending on regular/plain...