Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Validating with Regular Expressions | Custom Validation with JavaScript
JavaScript Form Validation

bookValidating with Regular Expressions

Validating form inputs often requires more than just checking for blank fields or simple length constraints. Many types of data—such as email addresses, phone numbers, and postal codes—follow specific patterns that are best checked with regular expressions (regex). Regular expressions are sequences of characters that define a search pattern, allowing you to match complex string formats in a concise and powerful way. In JavaScript form validation, regex patterns are essential for ensuring that user input matches the expected format, especially for fields where HTML5 pattern attributes are insufficient or too restrictive.

script.js

script.js

index.html

index.html

style.css

style.css

copy

In the validateEmail function above, the emailPattern variable holds a regex that checks if an email address is structured correctly. The pattern /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}$/ breaks down as follows:

  • ^ asserts the start of the string;
  • [a-zA-Z0-9._%+-]+ matches one or more allowed characters before the "@" symbol;
  • @ matches the literal "@" character;
  • [a-zA-Z0-9.-]+ matches one or more allowed characters for the domain name;
  • \. matches the literal dot character;
  • [a-z]{2,} matches at least two lowercase letters for the domain extension;
  • $ asserts the end of the string.

To test if an input value matches this pattern, use the test method. If emailPattern.test(email) returns true, the input is valid; otherwise, it is not. You can use similar regex patterns to validate other formats, such as phone numbers or postal codes, by adjusting the pattern to fit the required structure.

question mark

Which of the following regular expressions would correctly validate a U.S. phone number in the format "123-456-7890"?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain how to create a regex for validating phone numbers?

What are some common mistakes when writing regex for email validation?

Can you show examples of regex patterns for other types of form fields?

Awesome!

Completion rate improved to 5.56

bookValidating with Regular Expressions

Desliza para mostrar el menú

Validating form inputs often requires more than just checking for blank fields or simple length constraints. Many types of data—such as email addresses, phone numbers, and postal codes—follow specific patterns that are best checked with regular expressions (regex). Regular expressions are sequences of characters that define a search pattern, allowing you to match complex string formats in a concise and powerful way. In JavaScript form validation, regex patterns are essential for ensuring that user input matches the expected format, especially for fields where HTML5 pattern attributes are insufficient or too restrictive.

script.js

script.js

index.html

index.html

style.css

style.css

copy

In the validateEmail function above, the emailPattern variable holds a regex that checks if an email address is structured correctly. The pattern /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}$/ breaks down as follows:

  • ^ asserts the start of the string;
  • [a-zA-Z0-9._%+-]+ matches one or more allowed characters before the "@" symbol;
  • @ matches the literal "@" character;
  • [a-zA-Z0-9.-]+ matches one or more allowed characters for the domain name;
  • \. matches the literal dot character;
  • [a-z]{2,} matches at least two lowercase letters for the domain extension;
  • $ asserts the end of the string.

To test if an input value matches this pattern, use the test method. If emailPattern.test(email) returns true, the input is valid; otherwise, it is not. You can use similar regex patterns to validate other formats, such as phone numbers or postal codes, by adjusting the pattern to fit the required structure.

question mark

Which of the following regular expressions would correctly validate a U.S. phone number in the format "123-456-7890"?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
some-alt