0

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>
6
  • 2
    U have JS error !! function onSubmit() needs { for definition !! Commented Mar 11, 2014 at 4:39
  • You also don't need "return" from "return onSubmit()" in your form tag. That is delete the word return here. Should be "onsubmit="onSubmit()" Commented Mar 11, 2014 at 4:41
  • Sorry, i deleted the '{' when i formatted the code, is there now ehhe, this is not the problem. Commented Mar 11, 2014 at 4:42
  • I deleted the "return" word in the form tag but it still don't work. Commented Mar 11, 2014 at 4:47
  • you have used onclick event .. form doesnt have any onclick event put onsubmit .... Commented Mar 11, 2014 at 5:05

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

i tried but it's not entering the javascript function
0

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.

2 Comments

This is not true. You can submit a form with <button type=submit. That is valid.
It is valid but no recommended, it behaves incorrectly many times.
0

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

Comments

0

Use this code.. Working for me perfectly...

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

3 Comments

i tried your code but still without solution i will try to reset my cache let's see.
i m using chrome i tried with firefox to, but still with the problem
Wow, finally working!! I changed the function name, and now it's working, well i don't know why it happened but is working now man !! kk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.