Validating a File Name

There are many things that can be done with regular expressions, but one of the most useful is validating entry fields in forms on your Web pages. Script 8.3 expects the user to enter a valid URL of an image, and the regular expression helps to make sure that users do as you’ve requested (specifically, that there has to be a suffix that denotes an image file). Figure 8.3 shows the appearance of the page when an invalid entry was accidentally entered, and Figure 8.4 shows the result when the image name was typed correctly.

Figure 8.3. If the user enters something that isn’t a valid image file name, the page shows an error, thanks to regular expressions.

Figure 8.4. When the image name is entered correctly, the image ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.