0
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xmlns:p="http://www.springframework.org/schema/p"    
xmlns:context="http://www.springframework.org/schema/context"    
xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-3.0.xsd">    

<context:component-scan base-package="com.springcurd.*"></context:component-
scan>  

<bean 
class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>   
</bean>  

<bean id="ds" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>  
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  

<property name="username" value="system"></property>  
<property name="password" value="tiger"></property>  
</bean>  

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  

<bean id="dao" class="com.springcurd.dao.EmpDao">  
<property name="template" ref="jt"></property>  
</bean>  
<bean id="employeeValidator" 
class="com.springcurd.validator.EmployeeValidator">
</bean>

</beans>  

function validateForm() {
    var x = document.forms["emp"]["name"].value;
    var y = document.forms["emp"]["sal"].value;
    var z = document.forms["emp"]["designation"].value;

    if (x == "") {
        alert("Please enter Name");
        return false;
    }else if(!/^[a-zA-Z]*$/g.test(x)){
        alert("Only characters are allowed");
        return false;

    }else if(y==""){
        alert("Please enter Salary");
        return false;
    }else if (/[^0-9.]/g.test(y) ){
        alert("Only numbers are allowed");
        return false;

    }else if(z==""){
        alert("Please enter your Designation");
        return false;
    }else if (!/^[a-zA-Z]*$/g.test(z)) {
        alert("Only characters are allowed");
        return false;
    }
    }
    function nameValidate(){
    var a = document.forms["emp"]["name"].value;
    if(a == ""){
        alert("Please enter name");
    }else if (!/^[a-zA-Z]*$/g.test(a)){
        alert("Only characters are allowed");
    }else
        alert(a+" "+"Name entered successfully");
     return true;
    }
    function salValidate(){
    var b = document.forms["emp"]["sal"].value;
    if(b == "") {   
        alert("Please enter Salary");
    }else if (/[^0-9.]/g.test(b) ){
        alert("Only numbers are allowed");
    }else
        alert("Salary entered successfully");
    return true;
    }
    function desigValidate(){
     var c = document.forms["emp"]["designation"].value;
     if(c == ""){
         alert("Please enter designation");
     }else if (!/^[a-zA-Z]*$/g.test(c)){ 
            alert("Only characters are allowed");
     }else
         alert("designation entered successfully");
    return true;
    }
    function validate(){
    var d=document.getElementById("dropDown").value;
    alert("you selected"+" "+d);
    }
     <!DOCTYPE html>
     <html>
     <h1>Add New Employee</h1>

    <head>
    <script type="text/javascript" src="empForm.js">

    </script>

    </head>
    <body>
     <form name="emp" action="save.spring" onsubmit="return validateForm()" 
    method="POST" >

    <table >    
         <tr>    
          <td>Name : </td>   
          <td><input type="text"  name="name" onblur="nameValidate()"/></td>  
         </tr>    
         <tr>    
          <td>Salary :</td>    
          <td><input  type="text" name="sal" id="s" onblur="salValidate()"/>
    </td>  
         </tr>   
         <tr>    
          <td>Designation :</td>    
          <td><input  type="text" name="designation" onblur="desigValidate()"/>
    </td>  
         </tr>   
         <tr>    
          <td> </td>    
          <td><br><input type="submit" value="Save" /></td>    
         </tr>    
        </table> 

    </form>
    <select id="dropDown" onchange="validate()">
    <option value="Hyd">Hyd</option>
    <option value="Bnglr">Bnglr</option>
    <option value="Chennai">Chennai</option>
    </select>
    <body>
    </html>

my JS file not accessing with html.i used my JS file name in /springcurd/WebContent/WEB-INF/js/empForm.js path and HTML page in /springcurd/WebContent/WEB-INF/jsp/empform.jsp path.can ayone tell me what is the problem.

8
  • 1
    check it in the browser console. Commented May 26, 2017 at 5:18
  • Uncaught ReferenceError: nameValidate is not defined at HTMLInputElement.onblur (empform.spring:17) Commented May 26, 2017 at 5:37
  • gettin this in console when i run in browser Commented May 26, 2017 at 5:38
  • In the console., check the network info. to locate if your request the right js file path. Then modify id as the below answer suggests Commented May 26, 2017 at 5:44
  • i tried but not working Commented May 26, 2017 at 5:56

3 Answers 3

0

Path should be

    <script type="text/javascript" src="../js/empForm.js">

Try to force cache refresh your browser (Ctrl+F5) after applying changes.

Sign up to request clarification or add additional context in comments.

4 Comments

still not getting out put . when i run in browser consol showing the error like "Uncaught ReferenceError: nameValidate is not defined at HTMLInputElement.onblur (empform.spring:17)"
Are your functions (e.g. nameValidate) inside $(document).ready() or something? If so html will not be able to find your functions. Try putting them outside $(document).ready().
can you please explain my total code is mentioned above.
I believe that your js file can be found by your html but not the functions Instead of putting onblur on your html, try adding id to your elements then use addEventListener e.g. var s = document.getElementById("s"); s.addEventListener("blur", salValidate());
0

I think your script tag should be like this.You have to point to the proper directory to get the js file

<script type="text/javascript" src="springcurd/js/empForm.js">

1 Comment

try springcurd/js/empForm.js or even /springcurd/js/empForm.js
0

Because you are using the Spring framework your file structure should look like this as a best practice:

springcurd
|
├── resources
|   └── empForm.js
|
└── WEB-INF
    └── jsp
        └── empform.jsp

In your HTML code you should add the script like this:

<spring:url value="/resources/empForm.js" var="empFormJS" />
<script src="${empFormJS}"></script>

In the XML file which configures your bean, you should add these two lines at the end of the file before the </beans> tag.

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />

To see the changes please perform Project -> Clean and re-deploy your application.


Update

The XML file looks like the MVC definitions are missing. Would you, please, try to replace that code in the XML file and let me know if it works for you?

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:p="http://www.springframework.org/schema/p"    
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc = "http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context    
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

And at the end of the file change it like that:

    <mvc:resources mapping = "/resources/**" location="/resources/" />
    <mvc:annotation-driven/>

</beans>

If that still doesn't help try to register this bean additionally:

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

7 Comments

in web-inf i took two folders one for jsp and one for js . i change my js file into jsp folder but no use.
Can you please paste the URL, with which you are accessing the content of the empform.jsp? That would help me to figure the path out, because the path to the js file is normally relative to the requested URL.
Thank you, I've updated the answer. Does it help you now?
when i try to add those two lines in xml file it shows error like "The prefix "mvc" for element "mvc:resources" is not bound."
Would you mind to add the full content of the XML file to your original post?
|

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.