Reputation:
I have already checked previous answers on the SO. I know they are cool.
Here is my chance: I am taking values of textbox and passing it to function to validate. It passes url correctly but not doing validations. Can someone check where is the bug? I could not identify
<html>
<head>
<title>ThenWat</title>
<script>@Vicky: above regex does not filter:
function validateURL($URL) {
alert($URL);
$pattern_1 = "/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i";
$pattern_2 = "/^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i";
if(preg_match($pattern_1, $URL) || preg_match($pattern_2, $URL)){
alert("correct");
return true;
} else{
alert("Incorrect");
return false;
}
}
</script>
</head>
<body style="height: 560px">
<form>
<label>Enter URL: <input type="text" value="http://www.paulgraham.com/herd.html" name="sent" id="t1" style="width: 400px; height:40px;" ></label><br/>
<div style="z-index: 1; left: 420px; top: 160px; position: absolute; margin-top: 0px"> <button onclick="validateURL(document.getElementById('t1').value)";> Fire me </button>
</div>
</form>
</body>
</html>
UPDATE
<html>
<head>
<title>ThenWat</title>
<script>@Vicky: above regex does not filter:
function validateURL($URL) {
var str=$URL;;
var n=str.match(/((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g);
document.getElementById("demo").innerHTML=n;
//alert("correct or incorrect as per validation"); how to do this?
}
</script>
</head>
<body style="height: 560px">
<form>
<label>Enter URL: <input type="text" value="http://www.paulgraham.com/herd.html" name="sent" id="t1" style="width: 400px; height:40px;" ></label><br/>
<div style="z-index: 1; left: 420px; top: 160px; position: absolute; margin-top: 0px"> <button onclick="validateURL(document.getElementById('t1').value)";> Fire me </button>
</div>
</form>
</body>
</html>
Upvotes: 0
Views: 1680
Reputation: 3916
var str="The rain in SPAIN stays mainly in the plain";
var n=str.match(/ain/g);
To match string with regular expression in javascript you need to use match()
. Check http://www.w3schools.com/jsref/jsref_match.asp
regexp for URL : /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
Update:
function myFunction(){
var str="http://www.google.com";
var regexp = /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g;
if(str.match(regexp)){
alert("okay");
}else{
alert("not okay");
}
}
regular expression library : https://github.com/javaquery/regexp/blob/master/regexp-0.0.1.js
Upvotes: 0
Reputation: 1150
Try this:
function validateURL($URL) {
alert($URL);
$pattern_1 = /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i;
$pattern_2 = /^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i;
if($pattern_1.test($URL) || $pattern_2.test($URL)){
alert("correct");
return true;
} else{
alert("Incorrect");
return false;
}
}
Upvotes: 1
Reputation: 617
The problem for your is your regular expression
i have a pretty good one i normally use like this:
[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?
It checks first if something before the first . (ex. testing in testing.mywebsite.com) then checks mywebsite and last after the last dot it even checks the ending
Upvotes: 0
Reputation: 368
You can use $pattern1.test($URL) or $pattern1.match($URL)instead of using preg_match($pattern_1, $URL)
Upvotes: 1