In most browsers - those that support Java Script 1.5 (Firefox, Chrome, Safari, Opera 7 and Internet Explorer 8 and higher) - you can use more powerful regular expressions. The code presented above is fine in that it checks everything that we wanted to check, but uses a lot of code to test each requirement individually and present different error messages.We're going to show you now how to apply the password tests using a single regular expression.The red/green markers have been implemented using CSS: In this example it should be clear to the user that the form can only be submitted once all three green ticks appear.
All we have changed from the previous example is to add some extra attributes to the form input fields.
The rest of the HTML and Java Script remains unaltered: polyfill code which can be found further down the page.
Consider the following: If you are using a supported browser you can use the form below to test the regular expression: If you want to restrict the password to ONLY letters and numbers (no spaces or other characters) then only a slight change is required.
Instead of using is shorthand for 'any letter, number or the underscore character'.
The form below has three input fields: username, pwd1 and pwd2. If a false value is returned then the form submission is cancelled.