dongluanguang4774 2015-04-15 05:45
浏览 46
已采纳

如何检查url是否为空字符串

Hi there is there possible to validate the input tag with url type?

I currently have an input tag with URL type for user to key in the url, it is okay if the user key in value that is not a url.

But when user key in "http://www.", my input field accept it and posted the form to the server which is not a proper value. How to check the empty value in the input field to validate empty url?

Here are few scenario my form accepted the url link with empty url:

  1. http://www.
  2. www.
  3. http://

Is there any way to validate and stop the form to submit when there are empty value in the url link with javascript or php? thanks

My FORM's CODE:

<form class="form" id="form" action="" method="POST" enctype="multipart/form-data">
                                    <div class="box-body">
                                        <input type="hidden" name="banner_id" value="1"></input>
                                        <div class="form-group" >
                                            <label for="bannerName">Banner Name 旗帜名称</label>
                                            <input type="text" class="form-control" name="bannerName" id="bannerName" placeholder="Please Enter Name" onChange="checkDisabled(testing);">
                                        </div>
                                        <div class="form-group" >
                                            <label for="bannerUrl">Banner URL </label>
                                            <input type="url" autocomplete="on" class="form-control" name="bannerUrl" id="bannerUrl" placeholder="Please Enter Url" onChange="checkDisabled(testing);" required>
                                        </div>
                                         <div class="form-group">
                                            <label for="exampleInputFile">File input</label>
                                            <input type="file" id="uploaded_file" name="uploaded_file"  onChange="checkDisabled(testing);"><br>
                                            <p class="help-block">Your picture size not more than 2MB.  (Only JPEG or JPG is allowed)</p>
                                        </div>  

                                        <div class="checkbox">
                                            <button id="testing" type="submit" class="btn btn-primary" disabled>Update</button>      
                                        </div>
                                    </div><!-- /.box-body -->


                                </form>                  <!-- Date range -->
  • 写回答

5条回答 默认 最新

  • dongluzhi5208 2015-04-15 05:50
    关注

    If I'm not wrong to understand your question then this will work for you.

    Form page

    <div class="form-group" >
       <label for="bannerUrl">Banner URL </label>
       <input type="url" autocomplete="on" class="form-control" name="bannerUrl" id="bannerUrl" placeholder="Please Enter Url" onChange="checkDisabled(testing);" required>
       <span class="error_label"></span>
    </div>
    

    jQuery Way

    $('#form').validate({
        rules : {
            bannerUrl : {
                required : true,
                url : true
            }
        },
        messages : {
            bannerUrl : {
                required : "This field is required",
                url : "Please enter valid URL"
            }
        },
        submitHandler: function(form) {  
            if ($(form).valid()) 
                form.submit(); 
                return false; // prevent normal form posting
        },
        errorPlacement: function(error, element) {      
            $(element).closest('span').find('.error_label').html(error);
        }
    })
    

    Regex Way

    $('#bannerUrl').on('keyup', function (e) {
        var checkUrl = checkWebUrl($(this).val());
        console.log(checkUrl);
        if (!checkUrl) {
            // your error message
            alert('Please enter valid url');
            return false;
        } else {
            // your message
            alert('Valid url')
        }
    });
    
    function checkWebUrl(url)
    {
        //regular expression for URL
        //console.log(learnRegExp('http://www.google-com.123.com')); // true
        //console.log(learnRegExp('http://www.google-com.123')); // false
        //console.log(learnRegExp('https://www.google-com.com')); // true
        //console.log(learnRegExp('http://google-com.com')); // true
        //console.log(learnRegExp('http://google.com')); //true
        //console.log(learnRegExp('google.com')); //false
        //console.log(learnRegExp('www.google')); //false
        //console.log(learnRegExp('http://www.google')); //false
        var urlregex = new RegExp(
            "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
        return urlregex.test(checkWebUrl.arguments[0]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3