Validate Name Form

1

I have a question with validation of html5, I have little knowledge with regex I need to validate a form that only gets:

  • Upper and lower case letters, with accents and etc;
  • White spaces;
  • Can not accept only whitespace;

Follow the code:

<input type="text" name="nome" placeholder="Nome" minlength="4" pattern="[a-z\s]+$" required /> 

I'm using this code but pattern is not working.

    
asked by anonymous 17.04.2017 / 23:19

2 answers

2

The following code will allow a minimum of 2 characters and a maximum of 20 characters and not allow blank:

<input type="text" name="nome" placeholder="Nome" pattern="^[a-zA-Z][a-zA-Z0-9-_\.]{1,20}$" required>

If you do not want a number (only letters):

<input type="text" name="nome" placeholder="Nome" pattern="^[a-zA-Z][a-zA-Z-_\.]{1,20}$" required>

Allow at least 4 characters:

<input type="text" name="nome" placeholder="Nome" pattern="^[a-zA-Z][a-zA-Z-_\.]{3,20}$" required>

See some explanations:

  • ^ - Asserts position at the beginning of the string.

  • $ - Asserts position at the end of the string, or before the right line terminator at the end of the string (if any).

  • -_ - corresponds to a single character in the -_ (case-sensitive) list.

  • {1,20} - Quantifier - Matches between 1 and 20 times as many times as possible, returning as needed.

  • a-z - a single character in the range between one (ASCII 97) and z (ASCII 122) (case sensitive).

  • A-Z - a single character in the range between A (ASCII 65) and Z (ASCII 90) (case sensitive).

See more examples here: link .

jsfiddle: link

regex101: link

    
17.04.2017 / 23:55
2
<input type="text" required name="full_name" pattern="^[^-\s][a-zA-ZÀ-ú ]*">

Required field is required. In regex I do not allow anything that starts with space.

    
17.04.2017 / 23:43