开发者

How to validate an Internet address in an ASP.NET MVC textbox

How do I validate an Internet address in 开发者_运维问答an ASP.NET MVC textbox like www.gmail.com or http://www.hotmail.com?

I want to implement jQuery client-side validation.


If you just want to parse it, take a look a the Uri class.


I would suggest using jQuery Validation Plugin (which has build in url validation rule) with ASP.NET MVC 2 model validation. Here you can read how to create an asynchronous form in ASP.NET MVC which uses DataAnnotations and jQuery Validation Plugin (it also shows how to create validators which make use of built in client side rules of this plugin)


Here is a good way to validate internet address

var address = $('#vcr_LinkAddress').val();
var j = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
if (!j.test(address)) {
//If address is invalid
}
else
{
//address is valid
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜