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