-1

Our website has a form that requires* a signature field to be filled in to complete the form submission. Our problem is that we need to set a condition that disallows numbers, and symbols. We also need to disallow someone from placing their cursor in the signature field box and hitting the space bar as that is also currently accepted as a successful signature.

So to conclude we only want the signature field to be able to accept a first and last name and that would usually include a space between the two names and not allow people to ad anything else.

Thanks in advance if anyone could be helpful on this....

This is what we were trying to use but it disallowed any text in the signature field

function hasUnwantedCharacter(myString) {
  return /[`~!@#$%^&*()-_+{}[\]\\|,.//?;':"]/g.test(myString);
}

function hasUnwantedCharacter(myString) {
  return /[`~!@#$%^&*()-_+{}[\]\\|,.//?;':"]/g.test(myString);
}

Tried the above but it disallowed any text as well as symbols, numbers and spaces so obviously it was not correct.

Thanks for your reply Regex101 much appreciated. I have add your suggestion to the following code

function hasUnwantedCharacter(myString) {
  return /^[A-Za-z]+ [A-Za-z]+$/gm.test(myString);
}

$typeField.blur(function(e) {
  // console.log($typeField.val());
  let $typeFieldValue = $typeField.val().trim();

  if ($typeFieldValue == "" && !hasUnwantedCharacter($typeFieldValue)) {
    signaturePad.clear();
    tCtx.font = "50px Kalam";
    tCtx.fillText($typeField.val(), 20, 80);
    tCtx.fillStyle = "black";
    $hiddenInput.val(tCtx.canvas.toDataURL());
  }
});

But my form still doesn't accept spaces, I can do the "StackOverflow" and "StackOverflow" but not "Stack Overflow".

I don't have that much knowledge of javascript so any help is greatly appreciated. Thanks guys.

Paulio
  • 1
  • 1
  • Possible duplicate: https://stackoverflow.com/a/15472787/519413 – Rory McCrossan Mar 21 '23 at 09:44
  • 1
    Name only allow two words and doesnt allow name have single quote `'` like *O'Keefe Lastname* or three words name, right? Does it allow unicode name, and does it need capitalize? – dinhit Mar 21 '23 at 09:58

1 Answers1

0

A simple less strict version that only allow two words and a space:

^[A-Za-z]+ [A-Za-z]+$

Regex101 example

Quick explaination:

  • ^ start of line/string anchor
  • [A-Za-z]+ match any letter from a-z in both form uppercase and lowercase one or as many as possible time (+)
  • a space between
  • $ end of line/string anchor

A more strict version that need capitalize first character and only contains one uppercase character (first letter):

^[A-Z][a-z]* [A-Z][a-z]*$

Regex101 example

I split the word pattern into two parts: a uppercase character [A-Z] and follow by none or as many as possible (*) lowercase character [a-z]*

Or a version that would allow some name like McGeorge Bundy:

^([A-Z][a-z]*)?[A-Z][a-z]* [A-Z][a-z]*$

Regex101 example

Note: None of them allow name that contains unicode character

dinhit
  • 672
  • 1
  • 17