doujiexi1824 2014-02-17 13:46
浏览 95
已采纳

必填字段javascript将php中的下拉标签视为选定选项

I used a php form and a jquery script so if a customer leaves any field unfilled, they can't proceed. The related field,s code is as follows:

<div class="storagetype">
        <div class="input select">
         <div class="labelfortype"> <label for="storagetype">select your storage type</label></div><!--end of labelfortype class-->
                <select name="storagetype" id="storagetype">
            <option value="">(select)</option>
            <option value="business">business</option>
            <option value="domestic">domestic</option>
            <option value="student">student</option>
            </select>
    </div>
   </div><!--end of storagetype class-->

When nothing is selected, by default (select) option appears. So when (selected) is active a customer hasn't chosen one of the options yet. But still if customer wants to submit the form, the form lets them to do so since it sees (select) as an option with a value. How can I make sure that unless one of "business, domestic or student" options is selected, they can't submit the form? The live example can be found in this Link

The javascript code that I use for "required filll function" is as below:

 function formCheck(formobj){

    var x=document.forms["form1"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");

var fieldRequired = Array("storagetype", "location", "durationnumber", "duration", "txtDate",  "fname", "sname", "email", "number");
    // Enter field description to appear in the dialog box



    var fieldDescription = Array("Type of Storage",  "Post Code", "Duration Number", "Rental Duration Period",  "Rental Start Date", "First Name",  "Surname",  "Email Address", "Telephone Number");

    // dialog message
    var alertMsg = "Please complete all fields and enter a valid email address";

    var l_Msg = alertMsg.length;

    for (var i = 0; i < fieldRequired.length; i++){
        var obj = formobj.elements[fieldRequired[i]];
        if (obj){
            switch(obj.type){
            case "select-one":
                if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
                    alertMsg += " - " + fieldDescription[i] + "
";
                }
                break;
            case "select-multiple":
                if (obj.selectedIndex == -1){
                    alertMsg += " - " + fieldDescription[i] + "
";
                }
                break;

            case "text":
                case "textarea":
                if (obj.value == "" || obj.value == null || atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
                    alertMsg += "  " + " " + "
";
                }
                break;

            default:
            }
            if (obj.type == undefined){
                var blnchecked = false;
                for (var j = 0; j < obj.length; j++){
                    if (obj[j].checked){
                        blnchecked = true;
                    }
                }
                if (!blnchecked){
                    alertMsg += " - " + fieldDescription[i] + "
";
                }
            }
        }
    }

    if (alertMsg.length == l_Msg){
        return true;
    }else{
        alert(alertMsg);
        return false;
    }
    }
// -->
</script>

This javascript works great for all text fields but only gives the error that I've mentioned for the dropdown options such as storaget type in the example.

  • 写回答

2条回答 默认 最新

  • dongtao5104 2014-02-17 13:57
    关注

    change your validation javascript code inside loop

    case "select-one":
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].value == ""){ ---- } 
    //replace .text with .value
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛