简体   繁体   中英

client side form validation

I'm adding a new component in the Joomla front end. There, a form is given and I use JavaScript for form validation instead of Joomla Behaviour Validation because i need to print the error message in a span.

My form is:

<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>

How can I prevent submitting the form before clearing all the error messages?


EDIT

<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>

You have multiple elements with id="inv" where ID's must be unique.

Rename your spans as follows:

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

And reference with your JS:

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

Please note, this is far from an optimised or ideal way of handling form validation. Look at using jQuery Validation .

EDIT:

To prevent form submission on validation failure amend your form element as follows:

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

Then create a function validate() that will validate all your fields and return true if validation is passed or false if validation fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM