HTML5 Form Validation With Regex

There are two ideas that enters the scene now:

  • The tag has new type attribute values like url, email, date, telephone number, and color.
  • The tag has the new attribute pattern where you can describe allowed input with a regex.

Here are three pragmatic (but not globally perfect) examples I created:

Strong password: <input title="at least eight symbols containing at least one number, one lower, and one upper letter" type="text" pattern="(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,}" required />

Email address: <input type="text" title="email" required pattern="[^@]+@[^@]+.[a-zA-Z]{2,6}" />

Phone number: <input type="text" required pattern="(+?d[- .]*){7,13}" title="international, national or local phone number"/>

Generate random password in PHP

When creating web apps, there’s often a need to generate a random password for your users. It uses PHP’s handy str_shuffle() function:

function random_password( $length = 8 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
    $password = substr( str_shuffle( $chars ), 0, $length );
    return $password;

$password = random_password(8);