客户端表单验证

时间:2013-10-22 09:43:39

标签: javascript php validation joomla

我在Joomla前端添加了一个新组件。在那里,给出了一个表单,我使用JavaScript进行表单验证而不是Joomla行为验证,因为我需要在一个范围内打印错误消息。

我的表格是:

<form action="" method="post" name="downloadtender" id="downloadtender" >
    <br><br>
    <fieldset>
        Name:<br> 
        <input name='uname' type='text' id='uname'  onblur='valid(this)' /><span id='inv' style="color: red;font-size: 12px;">*</span>
        <br>
        Designation:<br>
        <input name='desig' type='text' id='desig' onblur='valid(this)'/><span id='inv' style="color: red;font-size: 12px;">*</span>
        <br>
        Company:<br>
        <input name='comp' type='text' id='comp' onblur='valid(this)'/><span id='inv' style="color: red;font-size: 12px;">*</span>
        <br>
        Mobile Number:<br>
        <input name='phone' type='text' id='phone'  onblur='valid(this)'/><span id='inv' style="color: red;font-size: 12px;">*</span>
        <br>
        Email id:<br>
        <input name='email' type='text' id='email' onblur='valid(this)'/><span id='inv' style="color: red;font-size: 12px;">* </span>
        <br><br>
        <input type="submit" name="submit1"  value="Register" />
    </fieldset>
    <?php echo JHTML::_( 'form.token' ); ?>
</form>

如何在清除所有错误消息之前阻止提交表单?


修改

<script type="text/javascript">

    function valid(elem){
        var input=[document.getElementById('uname'),document.getElementById('desig'),document.getElementById('comp'),document.getElementById('phone'),document.getElementById('email')];
        for(var i=0;i<input.length;i++){
            if(input[i].value==''&& elem.id==input[i].id){
                elem.focus();
                switch(input[i].id){
                    case 'uname':
                        document.getElementById('uname-inv').innerHTML="*Field Required*";
                        break;
                    case 'desig':
                        document.getElementById('desig-inv').innerHTML="*Field Required*";
                        break;
                    case 'comp':
                        document.getElementById('comp-inv').innerHTML="*Field Required*";
                        break;
                    case 'phone':
                        document.getElementById('phone-inv').innerHTML="*Field Required*";
                        break;
                    case 'email':
                        document.getElementById('email-inv').innerHTML="*Field Required*";
                        break;
                }
            }
            else{
                switch(input[i].id){
                    case 'uname':
                        document.getElementById('uname-inv').innerHTML="";
                        break;
                    case 'desig':
                        document.getElementById('desig-inv').innerHTML="";
                        break;
                    case 'comp':
                        document.getElementById('comp-inv').innerHTML="";
                        break;
                    case 'phone':
                        document.getElementById('phone-inv').innerHTML="";
                        break;
                    case 'email':
                        document.getElementById('email-inv').innerHTML="";
                        break;
                }
            }
        }

        if(document.getElementById('email').value!=''){
            var t=document.getElementById('email');
            var x=t.value.indexOf('@');
            var y=t.value.lastIndexOf('.');
            if((x<1)||(y<x+2)||(y+2>t.length)){
                $er=1;
                document.getElementById('email-inv').innerHTML="*Please provide correct email address*"; 
            }
            else{
                document.getElementById('email-inv').innerHTML=""; 
            }
        }
    }

</script>

1 个答案:

答案 0 :(得分:0)

您有多个id="inv"元素,其中ID必须是唯一的。

按如下方式重命名您的范围:

<span id="uname-inv" style="color: red;font-size: 12px;">*</span>

用你的JS引用:

case 'uname':
    document.getElementById('uname-inv').innerHTML="*Field Required*";
    break;

请注意,这远非处理表单验证的优化或理想方式。请查看使用jQuery Validation

修改

要防止在验证失败时提交表单,请按以下步骤修改form元素:

<form action="" method="post" name="downloadtender" id="downloadtender" onsubmit="return validate()">

然后创建一个函数validate(),它将验证您的所有字段,如果验证通过,则返回true;如果验证失败,则返回false