如何在不使用http的情况下进行url验证(或在验证通过后添加)? [英] How to make url validation without http ( or add it after validation passed )?

查看:99
本文介绍了如何在不使用http的情况下进行url验证(或在验证通过后添加)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使自定义网址验证规则生效.

I can't make my custom url validation rule work.

我要添加新规则:

 jQuery.validator.addMethod("complete_url", function(val, elem) {
// if no url, don't do anything
  return  /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/.test(val);
    });
 ...
 $("#new_website").validate({
    rules: {
          url: "complete_url",
         'website[url]': {
            url: true,
            required: true}
           }
        });

但它返回了我

   invalid URL

我在此处检查了正则表达式- http://www.rubular.com/.它正在运行,但是没有通过我的自定义验证程序.

I checked Regex here - http://www.rubular.com/. It is working, but failing it my custom validator.

我做错了什么?

推荐答案

请不要为此使用正则表达式.甚至不要使用jQuery验证程序使用的正则表达式,将来它可能会更改,并且您将无法利用更新.这是这样做的方法:

Please don't use regexes for this. Don't even use the same regex that the jQuery validator uses, it may change in the future and you won't be able to take advantage of the updates. This is the way to do this:

$.validator.addMethod('validUrl', function(value, element) {
        var url = $.validator.methods.url.bind(this);
        return url(value, element) || url('http://' + value, element);
    }, 'Please enter a valid URL');

这篇关于如何在不使用http的情况下进行url验证(或在验证通过后添加)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆