简体   繁体   中英

Custom clientside regex validation in an html form

I am trying to validate a form input type text. The value should be ending in ".tar.gz" and should have some keywords. Anyways I have the regex ready, but I am stuck on how to validate this without pressing submit. Kind of like how "pattern" attribute works. Any suggestions? this is the row which needs to be validated and the submit button already has a function to disable the button after 1st click. as shown below.

<tr> <td>Suite Name</td> <td><input type='text' name='same' id='sname' title='Default suite name format: autosuite_<signum>_timestamp'/></td> </tr> 

<input type='submit' name='launch' id='launch' onclick='setTimeout(disableFunction, 1)' value='Launch'/><br>

You can use the String.search() method like:

var str = document.getElementById("yourinputelementid").value;
var n = str.search("regexpression");

Will return -1 if it doesn't exist or the index of the match if it does exist. If you want to do validation without pressing submit call a method to test string onkeyup event of your input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM