Search code examples
javascriptformsvalidationform-fields

What am I doing wrong to validate a full name input in a form?


I tried using the code below for a Full Name validation in a single input field with a space between the texts(a space between the first and second name). But for some reasons I couldn't make it work! Will you guys please kindly have a look at it?

This is the code:

<script>
  lp.jQuery(function($) {
  
    var ruleID = 'fullNameValid';
    var field = 'full_name';
    var message = 'Please enter a valid full name';
  
    var rules = module.lp.form.data.validationRules[field];
  
    $.validator.addMethod(ruleID, function(value, field) {
      var valid = /^[a-zA-Z ]$/.test(value);
      return valid || (!rules.required && !value);
    }, message);
  
    rules[ruleID] = true;
  
  });
</script>
I have also tried using these options:

1.... var valid = /^[a-zA-Z ]$/.test(value);
2.... var valid = /^[a-zA-Z/s]$/.test(value);
3.... var valid = /^[a-zA-Z ]+$/.test(value);
4.... var valid = /^[a-zA-Z/s]+$/.test(value);
5.... var valid = /^([a-zA-Z ])$/.test(value));
6.... var valid = /^([a-zA-Z/s])$/.test(value));
7.... var valid = /^([a-zA-Z ]+)$/.test(value));
8.... var valid = /^([a-zA-Z/s]+)$/.test(value));

But the validation doesn't work! What am I doing wrong?

And this is my page: http://unbouncepages.com/pradaxa-one/

Thanks in advance.


Solution

  • var valid = /^[a-z]+ [a-z]+$/i

    Maybe this regex can help you. And you should trim the value at first.