2

I tried every solution from articles one, two , three yet in my sample project, I can still enter a number or spec char in the first name field.

The six+ things I tried:

$.validator.setDefaults({ submitHandler: function(form) { form.submit(); } });

jQuery.validator.addMethod("regex", function(value, element, param) { return value.match(new RegExp("^" + param + "$")); });
var ALPHA_REGEX = "[a-zA-Z]*";

jQuery.validator.addMethod("accept", function(value, element, param) {
  return value.match(new RegExp("." + param + "$"));
});

function isValid(value)
{
  var fieldNum = /^[a-z]+$/i;

  if ((value.match(fieldNum))) {
      return true;
  }
  else
  {
      return false;
  }

}

  $(document).ready(function() {
    $('#contact_form').bootstrapValidator({
                  fields: {
            first_name: 
            {
                validators: {
                field: { accept: "[a-zA-Z]+" },
                regex: ALPHA_REGEX,
                 lettersonly:true,
                required: true,
                pattern: "^[a-zA-Z_]*$",
                        stringLength: {
                        min: 2,

                    },
                        notEmpty: {
                        message: 'Please supply your first name',
                        callback: function(value, validator, $field) {
                            if (!isValid(value)) {
                              return {
                                valid: false,
                              };
                            }
                            else
                            {
                              return {
                                valid: true,
                              };    

                    }
                }


            },

I am sure I am missing something simple or possibly using a slightly outdated jQuery Library? EDIT: I actually just tried using other versions of the library to no success.

  • As I understand you use the `ALPHA_REGEX` which is `"[a-zA-Z]*"`. That means a letter zero or more times... am I right? Did you try to set + instead of *? – SeReGa Jan 26 '19 at 20:42
  • Just tried `var ALPHA_REGEX = "^[a-zA-Z_]*$";` and `var ALPHA_REGEX = "[a-zA-Z]+";` it also did not work. –  Jan 26 '19 at 20:46
  • And what is the value that you pass to the validation function? (the name you entered) – SeReGa Jan 26 '19 at 20:50
  • I did https://stackoverflow.com/questions/4115372/jquery-validate-plugin-adding-a-custom-validator-to-accept-letters-only solution but changed the ALPHA_REGEX to + instead of *. I am going to try the solution without the other solutions maybe theres too many "cooks in the kitchen" –  Jan 26 '19 at 20:54
  • ...if it is + and not * than that article above is wrong. –  Jan 26 '19 at 20:57
  • https://stackoverflow.com/questions/2794162/jquery-validation-plugin-accept-only-alphabetical-characters Think I’m missing the script from this article I found with 76 upvotes. Then you just add letters only. On my phone so will try later... –  Jan 26 '19 at 21:50

1 Answers1

0

I suggest you to try your REGEX on some "Regex Matcher".

After you have the right REGEX, make sure you are passing it to your function, and not another one.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
SeReGa
  • 1,219
  • 2
  • 11
  • 32
  • https://regexr.com/ using [a-zA-Z]* found only letters no spec chars so that part is right... using + actually did the same thing so you are both right. Now I see the confusion, ha. –  Jan 26 '19 at 21:02
  • It's actually not the same, when you use * you will accept empty strings. when you use + you must have at least one character – SeReGa Jan 26 '19 at 21:08
  • I have a min of 2 length set on my inputs (if that’s what you’re referring too). So I cannot accept an empty string though that info is good to know –  Jan 26 '19 at 21:28