PDA

View Full Version : regex validation



d-machine
08-08-2008, 05:03 PM
Hi,

I've got a Regex for url, which is:
/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/

How can I use it to check an input text in my form, (with an alert pop-up)?

Thanks ;)

rangana
08-09-2008, 02:04 AM
I don't think I (fully) understand you, but have a try:


<script type="text/javascript">
window.onload=function(){
document.getElementById('test').onblur=function(){
if(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/.test(this.value))alert('Congratulations. That\'s a correct URL.');
else alert('Sorry. That\'s an incorrect URL.');
}}
</script>
<label for="test">Type a URL here: </label><input type="text" id="test">


The function fires onblur (meanwhile).

Hope that helps.

d-machine
08-09-2008, 10:10 AM
Thank you! :)

Is there a simple way to change it to only show the "Sorry. That\'s an incorrect URL." alert? (because I don't need the first alert and I don't know how to edit it..)?

rangana
08-09-2008, 11:01 AM
Replace with this script instead:


<script type="text/javascript">
window.onload=function(){
document.getElementById('test').onblur=function(){
if(!(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/.test(this.value)))alert('Sorry. That\'s an incorrect URL.');
}}
</script>

d-machine
08-09-2008, 02:27 PM
Thank you !!

Twey
08-09-2008, 03:29 PM
http://user:pass@host:port.tld/