0

I have created a small form which have one field for input the url and the other one is submit button and i want to validate the input url through the javascript so that in case of correct url it will show an alert box.Is it possible with javascript? If yes , please help me to getting this through the below code.

<form name="lln-form" class="llnfrm" id="lln-form" id="bulk-form" action="" method="post" onsubmit="javascript:event.preventDefault();return  submit_ajax();">
<fieldset style="">
<label>Enter URL:</label>
<input type="text" name="url" placeholder = "enter your url">
</fieldset>
<input class="lln-button button button-primary" type="submit" name="lln-submit" id="llnbuton" value="Save" onsubmit = "saveURL()"; />
</form>
<script type="text/javascript">
function saveURL()
{
<?php
        if(isset($POST['url'])==true&&empty($POST['url'])==false){
$url=$POST['url'];
if(filter_var('$url',FILTER_VALIDATE_URL)==true)
    echo "VAlid URL";
}
    ?>
}

user6891871
  • 149
  • 1
  • 3
  • 11
  • http://stackoverflow.com/questions/4314741/url-regex-validation – Mahi Oct 18 '16 at 06:22
  • Possible duplicate of [Javascript regular expression to validate URL](http://stackoverflow.com/questions/8667070/javascript-regular-expression-to-validate-url) – NibblyPig Oct 18 '16 at 06:23
  • `if(filter_var($url,FILTER_VALIDATE_URL)==true)` instead of `if(filter_var('$url',FILTER_VALIDATE_URL)==true)` – Pradyut Manna Oct 18 '16 at 06:23
  • http://stackoverflow.com/questions/1303872/trying-to-validate-url-using-javascript duplication of thiso one. – Mitesh Shah Oct 18 '16 at 06:25
  • Possible duplicate of [Check if a Javascript string is a url](http://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url) – b.g Oct 18 '16 at 06:26

1 Answers1

1

You can try

var txturl=document.getElementById('TXTURL');
valid=txturl.value.search(/^([\w-]+(?:\.[\w-]+)*).((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2,4})?)$/i );
if(valid!=0){
    alert("Invalid URL");
}
Veer
  • 3,596
  • 1
  • 12
  • 12