I am trying to make a cookie that should contain a number value. It all work fine, unless the form that sends the info to my receiver page sends and empty string. Then something strange happens. It does not seem to be empty, so my error checker does not detect that it is empty (null or ""). So when i try to make the string into an integer the server get angry with me. If there is a good way to search a string for characters, in my case, return false unless it is a number (0,1,...9), it would help a lot. Or if there is a way to make sure the form does not send empty strings in the first place.

Ps. My code is correct, so I won't post it.

Thanks for the help!

Recommended Answers

All 4 Replies

Seriously, no one know?

You say that the form sends an empty string but your error checker doesn't detect empty strings? Have you tried debugging your code to see what *actually* the error checker is checking against?

There are a number of ways in which the problem can be rectified but without knowing the actual business context, suggesting an appropriate solution might be a bit difficult. How are you actually sending that value to the server? Setting it using Javascript? Sending it via hidden variables? Is that value mandatory i.e. should that value always be a digit?

As far as the solution is considered, there are a couple of them:
- If a check for blank string fails it means that the value sent consists of spaces. Try trimming the string before checking it for blank.
- You can always catch the NumberFormatException which is thrown when parsing the fails thereby handling all invalid input.

You say that the form sends an empty string but your error checker doesn't detect empty strings? Have you tried debugging your code to see what *actually* the error checker is checking against?

There are a number of ways in which the problem can be rectified but without knowing the actual business context, suggesting an appropriate solution might be a bit difficult. How are you actually sending that value to the server? Setting it using Javascript? Sending it via hidden variables? Is that value mandatory i.e. should that value always be a digit?

As far as the solution is considered, there are a couple of them:
- If a check for blank string fails it means that the value sent consists of spaces. Try trimming the string before checking it for blank.
- You can always catch the NumberFormatException which is thrown when parsing the fails thereby handling all invalid input.

I have actually managed to fix the problem of not getting an empty string. I needed to trim it, but my main question is how i can search for certain characters. I have been trying to find this out for quite some time now, and I just cant seem to find the answer anywhere. What i want to do is to search the string, and return a value if anythig in that string is not a number (0,1, ... ,9). So if someone fills in "abc" in my 3 digit html form, i want the string to return null.

Thanks for the help!

> What i want to do is to search the string

Depends on what kind of search it is; if it's just searching for a single character or a string you can use the overloaded versions of the indexOf method. If you need power searching i.e. search for digits only or search for a pattern which starts with a ! and ends with #, you need to use Regular Expressions, which I admit might take a bit of learning.

> So if someone fills in "abc" in my 3 digit html form, i want the string to
> return null

You are now talking about validation which is a specific application of string searching. You can use regular expressions for such purpose. But IMO you'd be better off using a validation library like Commons Validator which would relieve you of writing all the validation code yourself. But this again comes with a bit of learning curve.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.