My javascript function is not working, when i click to submit my form. can someone help with that? i think is everything all right, but it's not working for some reason i don't know.
<form action="<?php echo base_url();?>my_data" onsubmit="onSubmit()" method ="POST">
<p class = "my_data_text">Nome</p>
<input type="text" class="form-control" style="width:54%;" name= "changed_user_name" value = "<?php echo $this->session->userdata('nome_usuario');?>"></input>
<p class = "my_data_text">Email</p>
<input type="text" class="form-control" style="width:54%;" name = "changed_user_email" value = "<?php echo $this->session->userdata('email_usuario');?>"></input>
<p class = "my_data_text">Digite sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "changed_password"></input>
<p class = "my_data_text">Confirme sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "change_confirm_password"></input>
<button id ="btn_mydata_submit_incident" type="submit" class="btn btn-primary" >salvar</button>
</form>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
function onSubmit(){
alert("email inválido");
var result =1;
var checar = document.querySelector("#changed_user_email");
var test = checar.value.indexOf("@");
if (test == -1){
alert("email inválido");
result =0;
}
var checar = document.querySelector("#changed_password");
var passw= /^[A-Za-z]\w{7,14}$/;
if(!checar.value.match(passw)){
alert("password inválido");
result =0;
}
if (result ==0){
return (false);
}
else{
return (true);
}
}
</script>
Try this:
<form action="<?php echo base_url();?>my_data" onsubmit="return onSubmit()" method ="POST">
<p class = "my_data_text">Nome</p>
<input type="text" class="form-control" style="width:54%;" name= "changed_user_name" value = "<?php echo $this->session->userdata('nome_usuario');?>"></input>
<p class = "my_data_text">Email</p>
<input type="text" class="form-control" style="width:54%;" name = "changed_user_email" id="changed_user_email" value = "<?php echo $this->session->userdata('email_usuario');?>"></input>
<p class = "my_data_text">Digite sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "changed_password" id="changed_password"></input>
<p class = "my_data_text">Confirme sua nova senha</p>
<input type="password" class="form-control" style="width:54%;" name = "change_confirm_password"></input>
<button id ="btn_mydata_submit_incident" type="submit" class="btn btn-primary" >salvar</button>
</form>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
function onSubmit(){
var result =1;
var checar = document.getElementById("changed_user_email");
var test = checar.value.indexOf("@");
if (test == -1){
alert("email inválido");
result =0;
}
var checar = document.getElementById("changed_password");
var passw= /^[A-Za-z]\w{7,14}$/;
if(!checar.value.match(passw)){
alert("password inválido");
result =0;
}
if (result ==0){
return false;
}
else{
return true;
}
}
</script>
I think its because you're not using the input type="submit"
but using button type="submit"
.
You should replace the submit button for this:
<input type="submit" value="salvar" class="btn btn-primary" id="btn_mydata_submit_incident"/>
Or just change the onSubmit
javascript event for the onclick
one.
another way you can try doing is
$('#button_id').click(function(){
onSubmit(); //call the function,return something true false value from function return and depending on that submit the form
$('#form_id').submit();
});
I hope,it helps you !!!
使用此代码。完美为我工作...
<form action="" onsubmit="onSubmit()" method ="POST"> <p class = "my_data_text">Nome</p> <input type="text" class="form-control" style="width:54%;" name= "changed_user_name" value = ""></input> <p class = "my_data_text">Email</p> <input type="text" class="form-control" style="width:54%;" name = "changed_user_email" value = ""></input> <p class = "my_data_text">Digite sua nova senha</p> <input type="password" class="form-control" style="width:54%;" name = "changed_password"></input> <p class = "my_data_text">Confirme sua nova senha</p> <input type="password" class="form-control" style="width:54%;" name = "change_confirm_password"></input> <button id ="btn_mydata_submit_incident" type="submit" class="btn btn-primary" >salvar</button> </form> </div> </div> </div> </div> </section> <script type="text/javascript"> function onSubmit(){ alert("email inválido"); var result =1; var checar = document.querySelector("#changed_user_email"); var test = checar.value.indexOf("@"); if (test == -1){ alert("email inválido"); result = 0; return false; } var checar = document.querySelector("#changed_password"); var passw= /^[A-Za-z]\\w{7,14}$/; if(!checar.value.match(passw)){ alert("password inválido"); result =0; return false; } if (result == 0){ return (false); } else{ return (true); } } </script>
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.