Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I have an issue with regular expression. I need to validate for the following url

http://50.244.41.23/ or http://myserver/

I have used following expressions

C#
Regex urlRx = new Regex(@"(http|https)://([0-9]*)", RegexOptions.IgnoreCase);

Regex urlRx = new Regex(@"(http|https)://(\b(?:\d{1,3}\.){3}\d{1,3}\b)", RegexOptions.IgnoreCase);


Both are working up to an extent, But it doesn't serve the exact purpose. It gives success for the following strings, But i want the expression to fail for such strings..

http://50.244.41.23\

http://256.244.41.23/

http://256.244.41.23.123/


Can someone help to create an apt regex validation for the above url.


Thanks
Sebastian
Posted
Comments
Peter Leow 9-Apr-14 3:36am    
You only want to validate these 2 strings? Any other values?
"http://50.244.41.23/" or "http://myserver/"
Sebastian T Xavier 14-Apr-14 8:22am    
I need to validate the port also; How can I modify this expression(the expression in my result) to accept port also?
Why http://256.244.41.23/ OR http://256.244.41.23.123/ are not valid?

What is that Logic?
Sebastian T Xavier 9-Apr-14 6:32am    
Both are not valid based on IPV4 standards....

Text this: Verified by me:


HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regex for urls with or without http Page</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#btnValidate').click(function() {
var txt = $('#txturl').val();
var re = /(http(s)?:\\)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/
if (re.test(txt)) {
alert('Valid URL')
}
else {
alert('Please Enter Valid URL');
return false;
}
})
})
</script>
</head>
<body>
<div>
Enter URL :<input type="text" id="txturl" />
<input type="button" id="btnValidate" value="Validate" />
</div>
</body>
</html>
 
Share this answer
 
v2
Comments
Sebastian T Xavier 14-Apr-14 8:21am    
Please see my solution above; But I need to validate the port also; How can I modify this expression to accept port also?
Hi All,

I have solved this case by using the following regex expression...

XML
^https?://(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/$


Thanks for support
Sebastian
 
Share this answer
 
v2
Comments
Sebastian T Xavier 14-Apr-14 8:20am    
I need to validate the port also; How can I modify this expression to accept port also?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900