|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am trying to validate a file name field where I only want certain image extensions to be entered...say...
png, jpg, gif, and tif how would i check teh substring...do i use indexOf()? please provide syntax..thanx |
|
#2
|
|||
|
|||
|
Regular expressions can make form validation a snap. You can them use to validate fields in ways you've never imagined. I made a little page for myself to help me write them. It's based on an article at this site. If you want to change the expression though, you have to do it in the html. I'm gonna put the page I wrote in here with a regular expression that only accepts the four extensions you listed in your post.
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><html> <head> <script language=JavaScript><!-- function Validate() { // obtain form value into variable var thing= document.all.thing_to_test.value; // define regex //for currency var pattern = /(^[$d])+([,d])+([d]$)/; var pattern = /.jpg$|.gif$|.png$|.tif$/ // test for pattern flag = pattern.test(thing); // pattern.test returns a boolean if(flag) { document.all.results.value="pattern matches" //return true; use these for a form } else { document.all.results.value="pattern doesn't match" //return false; } } --></script> <style><!-- td, button { font-size:10px; font-family:Arial; } --></style> </head> <body bgcolor="0099ff"> <table bgcolor=000000><tr><td> <table bgcolor=ffff00> <tr><td colspan=2> Regular Expression Tester </td></tr> <tr><td> test this </td><td> <input name="thing_to_test" type="text"> </td></tr> <tr><td> result </td><td> <input name="results" type=text> </td></tr> <tr><td colspan=2> <button style="background-color:0099ff;font-weight:bold;width:220;" onclick="Validate();">Test The RegEx Pattern</button> </td></tr> </table> </td></tr></table> </body> </html>[/code] Here's the link to the article: http://www.devshed.com/Server_Side/...tration/RegExp/ Have Fun. (That thing's untested in netscape, and of course it won't work with the document.all...) [This message has been edited by billyo (edited July 13, 2000).] |
![]() |
| Viewing: Dev Shed Forums > Web Design > HTML Programming > form validation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|