Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 5.56

bookValidating with Regular Expressions

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 3
some-alt