Click here to Skip to main content
16,016,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys I need some help with a web application I'm trying to create, it is a simple JSP page where you can insert, delete or update rows in a table stored in a data base. However, I need to use Java Script to validate the fields before sending them to the server. Here is the JSP code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    
    
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>New Record</title>
        
    </head>
<body>
        <script type="text/javascript">
            function validate(){
                alert("Your data is about to be validate!");
                //validate data
            return false;
            </script>
        <h1>New Record</h1>
        
        <form name="frmInNew" action="NewWS"  önsubmit="validate()" method="POST">
        
        Enter data in the fields
        <p>
            Number <input type="text" name="txtNumber" value="" size="20" />
        <p>
            Last Name <input type="text" name="txtLN" value="" size ="20"/>
        <p>
            Middle Name: <input type="text" name="txtMN" value="" size="20" />
        <p>
            Name  <input type="text" name="txtN" value="" size="20" />
        <p>
            Birth date <input type="text" name="txtBD" value="" size="20" />
        <p>
            Salary: <input type="text" name="txtSalary" value="" size="20" />          
        <p>
         <input type="submit" value="Send" name="btnSend"/>   
        </form>
    </body>
    
</html>

The issue is not the validation itself, the issue is I cannot even call the function correctly, as you can see I put an alert to test if the function was being called correctly but the alert does not pop up.
I cannot find what I'm doing wrong, as you can see I'm very new in JS so I hope you can help me on this one. Thanks
Posted

1 solution

Issues I Can See

1. [As reported by Sergey Alexandrovich Kryukov] -> that is onsubmit, not önsubmit.

2. As you are returning boolean value from function, you should write a return statement in the call like below (good practice).
HTML
onsubmit="return validate();"


3. Missing closing curly braces in the function.
JavaScript
function validate(){
      alert("Your data is about to be validate!");
      //validate data
      
      return false;
} // This is missing.



Suggestion

See the console window of FireBug in FireFox and check if you have any other errors.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Jun-13 15:02pm    
Good catch, a 5. What could really happen: the code is not interpreted at all, due to the syntax error, and the error is not exposed to the user, so nothing at all happens.
As I commented before, "önsubmit" is not the OP's bug, this is just the artifact of the CodeProject formatting script; so in OP's code it must be spelled correctly.
—SA
Yes exactly... :)

Thanks a lot Sergey Alexandrovich Kryukov... :)
Sergey Alexandrovich Kryukov 10-Jun-13 15:08pm    
You are very welcome; deserved...
Cheers,
—SA
:) :) Cheers.
Hanzel Doullery de Garcia 10-Jun-13 15:18pm    
Yes it totally solved!
I thank you both

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900