Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:48:17+00:00 2026-06-17T22:48:17+00:00

My requirement is that i want validation on my form field i m doing

  • 0

My requirement is that i want validation on my form field i m doing so using spring validator class and regex so what i m doing here, i am validating my DepartmentNamefield which is in String contains no numeric value any where. This validation i m performing using regex expression [0-9] because if it contains any numeric value then matcher.find() return true if it return true i am throwing error message .So problem i am facing is that when i am providing the string with non numeric value validation is done but if i m providing the pure string then still its throwing same message if i run the application again with providing pure String value then its working, but if again i am providing wrong entry ,validation is happening but after that if m providing the correct entry the same message throw so every time i need to run my application please resolve this issue

here is my validator class

package com.ankur.tutorial.validator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.nousinfo.tutorial.service.model.DepartmentBO;

public class DepartmentValidator implements Validator {

    boolean found = false;

    public boolean supports(Class<?> arg0) {

        return DepartmentBO.class.isAssignableFrom(arg0);
    }

    public void validate(Object object, Errors errors) {
        DepartmentBO departmentBO = (DepartmentBO) (object);
        System.out.println(departmentBO.getDepartmentName());

        if (departmentBO.getDepartmentName().equals("")) {

            errors.rejectValue("departmentName", "department.Name");
        } else {
            Pattern pattern = Pattern.compile("[0-9]");
            Matcher matcher = pattern.matcher(departmentBO.getDepartmentName());
            while (matcher.find()) {
                found = true;

            }
            System.out.println(found);
            if (found) {
                errors.rejectValue("departmentName", "department.string");
            }

        }

    }

}

this is my controller

package com.nousinfo.tutorial.controllers;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.nousinfo.tutorial.model.DepartmentForm;
import com.nousinfo.tutorial.service.impl.DepartmentServiceImpl;
import com.nousinfo.tutorial.service.model.DepartmentBO;
import com.nousinfo.tutorial.validator.DepartmentValidator;


@Controller
@RequestMapping("departmentController")
public class DepartmentController {
    private DepartmentServiceImpl departmentServiceImpl;

    private DepartmentValidator departmentValidator;

    public DepartmentServiceImpl getDepartmentServiceImpl() {
        return departmentServiceImpl;
    }

    public void setDepartmentServiceImpl(
            DepartmentServiceImpl departmentServiceImpl) {
        this.departmentServiceImpl = departmentServiceImpl;
    }

    public DepartmentValidator getDepartmentValidator() {
        return departmentValidator;
    }

    public void setDepartmentValidator(DepartmentValidator departmentValidator) {
        this.departmentValidator = departmentValidator;
    }

    /**
     * Set to set the view
     * 
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/departmentForm", method = RequestMethod.GET)
    public String view(Model model) throws Exception {
        DepartmentBO departmentBO = new DepartmentBO();
        model.addAttribute("departmentBO", departmentBO);
        return "departmentForm";
    }

    /**
     * Create the department
     * 
     * @param departmentForm
     * @param bindingResult
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/createDepartment", method = RequestMethod.POST)
    public ModelAndView createEmployee(
            @ModelAttribute("departmentBO") DepartmentBO departmentBO,
            BindingResult bindingResult) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        departmentValidator.validate(departmentBO, bindingResult);
        if (bindingResult.hasErrors()) {
            modelAndView.setViewName("departmentForm");
            return modelAndView;
        }

        modelAndView.addObject("departmentBO", departmentBO);

        if (departmentBO.getUpdateStatus() == 'A') {
            boolean flag = departmentServiceImpl.actionDecider(departmentBO);
            if (flag == false)
                modelAndView.setViewName("DBError");
            else
                modelAndView.setViewName("Success");

        }
        return modelAndView;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:48:18+00:00Added an answer on June 17, 2026 at 10:48 pm

    You have the variable found as a class property. Validators are singleton. Move the variable found inside the validate method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement that calls for a custom validator. The users want a
I find that jQuery validation plugin regex to be insufficient for my requirement. It
I want to do validation of a password field on a login form. The
I have a requirement for a validator class to run a series of validation
I am trying to validate my form Data using Hibernate Validator in my Spring
I have a requirement that I want to check the request headers and according
I have a requirement that i want to get all the system services running
My requirement is that I want an object (tee) to update if there have
I am working on asp.net mvc3 and i want a particular requirement that the
I am using an Ajax.BeginForm with unobtrusive validation. I want to give the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.