0

I am trying to implement java code on a web based application to reduce redundancy and increase OOP principles

I have a phone validation regular expression on server side java code. I have a requirement to perform the same validation on the browser as people are adding information, without communicating with the server. I understand I can place the same code into the javascript but then I have two locations to maintain the same code. I am looking for a simple way to send the regular expression function with the http response.

//java code
Class myVerf
{
    bool verPhone(input) {return //comepare verPhone to regEx}
}

//html
input type = "text" id = "1" onkeypress = "verPhoneFunc()"

//javascript
function verPhoneFunc()
{
    //get value from id 1
    //execute verPhone java code
}

Are there any downfalls to doing this? EX: what happens if there is no JVM on the machine the browser is running in? How does the java code execute on the browser?

I want to emphasise again... I cannot use AJAX because I can not communicate with the server.

3
  • Not sure what you mean no ajax. Are you saying that you want to send the validation info with the original reponse, i.e. the one containing the Javascript and form that is being validated? Also, are you using a web application framework for this app? Commented Nov 30, 2012 at 18:53
  • That is exactly what I want to do, currently using struts w/ tiles but may convert to spring MVC in the future Commented Nov 30, 2012 at 18:57
  • I think Struts 2 has support for congiruing your declarative validation rules in one place and that get's propagated to both server side validation and client side, in the fashion you suggest. Commented Nov 30, 2012 at 19:55

3 Answers 3

2

There's not really a way to do what you're looking for, without some additional technologies.

The most direct way to do what you want is to prepare a string in some common location that both the Java and the Javascript can access it. For sake of discussion, I'll say this is in a textfile somewhere but if you're using (for instance) JSP, you could prepare the string in Java and publish it to Javascript via your JSP template. The string would contain the (uncompiled) regular expression.

In this solution you have retained your main design goal (keep the regex maintained in only one place.)

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

1 Comment

this gave me the best solution for my needs. Thanks!
1

You can't easily and reliably share java code between your server-side app and the client app (like you said, what if there is no java installed; there are other reasons why this isn't a good approach as well).

Having said that, what you can do instead is share validation business rules. Define a language for specifying validation rules, then implement two validation engines that can read and execute these rules: one in Java for the backend and one in Javascript for the client. Your webapp can have a centrally managed repository of these rules and consistently feed them to both engines. The javascript engine can receive these rules embedded into the page's HTML, so no AJAX is necessary.

Comments

1

If you are using Struts 2, for instance, you can leverage that framework's built in declarative validation mechanism. This supports declaration of validation in one location, and the framework propagates that validation to both server side validations and client side validations.

http://struts.apache.org/2.3.4.1/docs/validation.html

5 Comments

What indicator do you get when you fail a validation requirement?
On the server side you get redirected to your input page. On the client side, I suspect you get a configurable error message displayed to the user.
Unfortunately, as I researched the Struts 2 verification, it all happened in the post http request, which wasn't what I was looking for. It provided me a great way to handle server side validation, but It did not meet full requirements. still +1
@MattWestlake This isn't what you want?
I had to be able to do client side validation w/o sending data to the server. With this method, no one knows knows if the data entered is valid until the post is submitted. He wanted real-time update verification as you were typing

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.