[英]how to display error message without alert using javascript
how to display the error message without alert using javascript below shown my code 如何使用下面显示的javascript显示错误消息而不会发出警告
function validateForm() { 函数validateForm(){
if (document.getElementById('email').value.trim()=="")
{
alert("Please enter your Email");
document.getElementById('email').focus();
return false;
}
if (document.getElementById('password').value.trim()=="")
{
alert("Please enter your Password");
document.getElementById('password').focus();
return false;
}
}
</script>
my form model code shown in below how to display the error message below the text field 我的表单模型代码如下所示,如何在文本字段下方显示错误消息
<form id="register-form" onsubmit ="return validateForm()" action="<?php echo base_url(); ?>Index.php/Login_cntrl/login" method="POST" >
<div class="field-wrap">
<label class="view-label">Email Address</label>
<input type="email" placeholder="Email Address" name="email" id="email" class="input-control" value=""/>
</div>
<div class="field-wrap">
<input type="password" placeholder="Password" name="password" id="password" value="" />
<a href="javascript:void(0)" class="btn btn-link btn-nobg" id="btn-show-forgot" >Forgot ?</a>
</div>
<div class="field-wrap">
<button type="submit" class="btn btn-submit" name="ulogin" id="ulogin" value="ulogin" >Login</button>
</div>
<div class="field-wrap">
<a href="javascript:void(0)" class="btn btn-link btn-nobg" id="btn-show-signup">NEW User? Sign up</a>
</div>
</form>
according to you modify my code but still it is not working 根据您的修改我的代码,但仍然无法正常工作
<script>
function validateForm() {
if (document.myform.email.value == "") {
document.getElementById('errors').innerHTML="Please enter a username"; return false;
}}
</script>
Assuming you are using bootstrap; 假设您正在使用引导程序; you can use a simple alert message like this 您可以使用这样的简单警报消息
HTML 的HTML
<div id="error" class="alert alert-danger" role="alert"></div>
Javascript Java脚本
document.getElementById('error').innerHTML="Please enter a username";
Replace everywhere you are calling alert
with javascript code above. 用上面的javascript代码替换您要调用alert
。
Reference: http://getbootstrap.com/components/#alerts 参考: http : //getbootstrap.com/components/#alerts
I'll do this using error div onto form... 我将使用error div来完成此操作...
<form id="register-form" onsubmit ="return validateForm()" action="<?php echo base_url(); ?>Index.php/Login_cntrl/login" method="POST" >
<div class="field-wrap">
<label class="view-label">Email Address</label>
<input type="email" placeholder="Email Address" name="email" id="email" class="input-control" value=""/>
</div>
<div class="field-wrap">
<input type="password" placeholder="Password" name="password" id="password" value="" />
<a href="javascript:void(0)" class="btn btn-link btn-nobg" id="btn-show-forgot" >Forgot ?</a>
</div>
<div class="field-wrap">
<button type="submit" class="btn btn-submit" name="ulogin" id="ulogin" value="ulogin" >Login</button>
</div>
<div class="field-wrap">
<a href="javascript:void(0)" class="btn btn-link btn-nobg" id="btn-show-signup">NEW User? Sign up</a>
</div>
<div id='errorDiv' class='col-xs-12 pull-right'> </div>
</form>
/// JS /// /// JS ///
function validateForm() {
if (document.getElementById('email').value.trim()=="")
{
document.getElementById('errorDiv').innerHTML = "Please enter your Email";
document.getElementById('email').focus();
return false;
}
if (document.getElementById('password').value.trim()=="")
{
document.getElementById('errorDiv').innerHTML = "Please enter your Password";
document.getElementById('password').focus();
return false;
}
}
</script>
Here is full markup: using error option that comes with bootstrap; 这是完整的标记:使用引导程序附带的错误选项;
<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<form id="register-form" onsubmit ="return validateForm()" method="POST" >
<div class="field-wrap">
<label class="view-label">Email Address</label>
<input type="email" placeholder="Email Address" name="email" id="email" class="input-control" value=""/>
</div>
<div class="field-wrap">
<input type="password" placeholder="Password" name="password" id="password" value="" />
<a href="javascript:void(0)" class="btn btn-link btn-nobg" id="btn-show-forgot" >Forgot ?</a>
</div>
<div class="field-wrap">
<button type="submit" class="btn btn-submit" name="ulogin" id="ulogin" value="ulogin" >Login</button>
</div>
<div class="field-wrap">
<a href="javascript:void(0)" class="btn btn-link btn-nobg" id="btn-show-signup">NEW User? Sign up</a>
</div>
<div id="error" class="alert alert-danger" role="alert"></div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript">
function validateForm() {
if (document.getElementById('email').value.trim()=="")
{
document.getElementById('error').innerHTML = "Please enter your Email";
document.getElementById('email').focus();
return false;
}
if (document.getElementById('password').value.trim()=="")
{
document.getElementById('error').innerHTML = "Please enter your Password";
document.getElementById('password').focus();
return false;
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.