javascript - 如何根据javascript中的打开输入来验证文本框

标签 javascript php jquery html mysql

我正在构建我的网站,我正在为用户提供在那里构建自定义 URL 的选项。我正在从我的数据库 mysql 中验证这个自定义 url。如果自定义 url 已经存在,那么他们必须在那里更改 customurl。如果用户不想要自定义 url,他们将不会选择该单选按钮。

我的问题是我的代码正在验证文本框,即使是那些他们没有选择 customurl 选项的文本框,因此表单没有提交。

这是我的代码:

    <html>
    <body>
     <form action="insertdata.php" id="contact-form1" class="mar_l10" method="post">
     <p><input type="radio" name="cust1" value="2" onclick="hide();" class="display_inline">Dont want</p>
     <p><input type="radio" name="cust2" value="3" onclick="show();" class="display_inline">Custom URL need</p>
     <p id="showlabel" style="display: none;" onblur="test(this);">Custom URL</p>
    <input type="text"  name="url" id="url" style="display: none;" onblur="test(this);" class="display_inline" maxlength="20"/>
<div id="user_status"></div>
     <input type="submit" id="continue_details" value="Continue" title="Continue" class="submit_btn" >   
     </form>



     <script type="text/javascript">
            function show()
            {
            document.getElementById('url').style.display = 'block'; 
            document.getElementById('hostname').style.display = 'block';
            document.getElementById('checkurl').style.display = 'block'; 
            document.getElementById('showlabel').style.display = 'block';
            }
             function hide() 
             { 
             document.getElementById('url').style.display = 'none'; 
                 document.getElementById('hostname').style.display = 'none';
             document.getElementById('checkurl').style.display = 'none'; 
             document.getElementById('showlabel').style.display = 'none';
             }



    var flag = true;
    $('#contact-form1').submit(function(e){
    e.preventDefault();

    if($('#user_status').val() == " "){
     document.getElementById('contact-form1').submit();
        //return true;
    }
    else
    {
    test();
    $('.user_status').show();

    }




    })



    function test()
    {   
        var username = document.getElementById('url').value;
        var url = "check_url.php?username="+username;

       if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {

                var result = xmlhttp.responseText;
                if(xmlhttp.responseText =='Custom URL is available')
                {
                    document.getElementById('user_status').innerHTML =result ;

                    document.getElementById('contact-form1').submit();

                }
                else
                {
                   document.getElementById('user_status').innerHTML =result ;
                }

            }
        }


        xmlhttp.open("GET",url,true);

        xmlhttp.send();

    }
     </script>



     </body>
     </html>

对于自定义 url 选项,单击数据库验证也正常工作并且表单也在提交。但如果我选择不需要自定义 url 选项,表单也会验证 url 文本框。

请任何人帮助我解决这个问题。

谢谢。

最佳答案

在调用测试函数之前必须检查单选按钮,如下所示:

else if ($('form input[type=radio]:checked').val() == 3) 

关于javascript - 如何根据javascript中的打开输入来验证文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22626503/

相关文章:

php - 日期问题()

jquery - 同一页面中的两个jquery分页插件似乎不起作用

javascript - window.open 兼容所有浏览器吗?

javascript - 如何编译和评估对象上指定的逻辑运算符和操作数?

javascript - Highcharts 实体量表 : How can I disable gradient fill?

php - 替换表中所有现有数据

php - 单个数据库或多个数据库(用于单个应用程序中的多个用户)

javascript:读取纯 html 字符串并使用 DOMparser 更改链接路径

javascript - 从下拉菜单输出差异时获取空值? (Javascript)

javascript - JavaScript 对象和函数的区别