Validating a String in Java

R

Ronan40060

Dear Friends

I need to write a small peice of code a string "payment_code" which
comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed
If a user enters characters other than the mentioned above , the
alert
message should be displayed

This requirement is a part of Reverse Enginnering project that we are
doing , the current script is written to validate above is in PB and
it needs to be written in Java

I also had writing the same in Java Script but then due to the
framework of our project , it demands to be written in Java


typical steps would be
1 declare variables of integer types
var li_len, li_idx, li_value
2 declar variables of string types
var ls_tmp_string, ls_char
3 Remove the white spaces from as_payment_code string and store it in
ls_tmp_string
4 Calulate the length of the string variable ls_tmp_string and store
it in li_len
li_len = ls_tmp_string.length;
5 For li_idx = 1 to li_len
get the substring and store it in ls_char
var ls_char = ls_tmp_string.substr(li_idx,1);
Check ls_char for 0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop
'.',Comma ',',Plus '+'
if it cotains any of the above values then continue
else
show a pop-up and display message ( "Error!", "Invalid character: "
+ ls_char ) and return false
Thank you
 
G

Gordon Beaton

I need to write a small peice of code a string "payment_code" which
comes populated from a text field , should contain only
0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed If a user enters
characters other than the mentioned above , the alert message should
be displayed

This sounds a lot like homework, but the "obvious" solution is to
simply match your input string against a regular expression consisting
of the set of valid characters. Expect to spend about 2 lines of code
on this.

/gordon

--
 
C

Chris ( Val )

Dear Friends

I need to write a small peice of code a string "payment_code"
which comes populated from a text field , should contain only
0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed
[snip]

typical steps would be
1 declare variables of integer types
var li_len, li_idx, li_value
2 declar variables of string types
var ls_tmp_string, ls_char

[snip]

I agree with Gordon, but I want to mention that
you *should* make an attempt to produce the work
yourself first, if you really want to learn,
because no one will write the program for you.

If you show an attempt of some sort, then you
will more than likely get the guidence you need
to help you complete it.

On a final note, *please* try to choose much
better names for your identifiers, because they
are anything but appropriate, imho.
 
L

Lew

On a final note, *please* try to choose much
better names for your identifiers, because they
are anything but appropriate, imho.

Follow Sun's suggesting coding conventions for variable names - lower-case
first letter, camelCase for remaining compound word parts (for non-constants),
with no underscores. Make the names meaningful and not reflective of their
implementation (no "string" or "char" name parts).
 
D

Daniel Pitts

Ronan40060 said:
Dear Friends

I need to write a small peice of code a string "payment_code" which
comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed
If a user enters characters other than the mentioned above , the
alert
message should be displayed

This requirement is a part of Reverse Enginnering project that we are
doing , the current script is written to validate above is in PB and
it needs to be written in Java

I also had writing the same in Java Script but then due to the
framework of our project , it demands to be written in Java


typical steps would be
[snip]
Did you have any actual questions? Anyway, here is some advice related
to your post.

You can use the Pattern class to determine if your string matches those
characters using Regular Expressions.

As for JavaScript vs Java, you should ALWAYS do validation on the server
side. If possible, also add it to the client side. The problem with
JavaScript validation is that the client might disable or circumvent the
JavaScript validation.

Good luck,
Daniel.
 
C

Chris ( Val )

Follow Sun's suggesting coding conventions for variable names - lower-case
first letter, camelCase for remaining compound word parts (for non-constants),
with no underscores. Make the names meaningful and not reflective of their
implementation (no "string" or "char" name parts).

Yes, I agree.

I don't think anyone could go too far wrong by adhering to
Sun' naming conventions.
 
W

Wojtek

Gordon Beaton wrote :
This sounds a lot like homework,

I alwyas get this urge to write the solution, but make it so convoluted
and cutting edge, so that it is obviously not written by a student.

You know, lots if interfaces, long chains of extended classes, factory
methods for everything, class loaders, method chains with one or two
lines which call other methods, and so on.
 
G

Gordon Beaton

Gordon Beaton wrote :

I alwyas get this urge to write the solution, but make it so convoluted
and cutting edge, so that it is obviously not written by a student.

That's a fine tradition practiced on many other programming groups.

/gordon

--
 
R

Roedy Green

That's a fine tradition practiced on many other programming groups.

The problem with that approach is not just your victim reads it. You
are in effect creating a Monty Python Hungarian phrase book.
 
L

Lew

Wojtek said:
Gordon Beaton wrote :

I alwyas get this urge to write the solution, but make it so convoluted
and cutting edge, so that it is obviously not written by a student.

You know, lots if interfaces, long chains of extended classes, factory
methods for everything, class loaders, method chains with one or two
lines which call other methods, and so on.

Full Javadocs, implement java.io.Serializable fully and correctly, use lots of
anonymous inner classes, annotations, NIO and java.util.concurrent, ...
 
B

Bas

As for JavaScript vs Java, you should ALWAYS do validation on the server
side.

All the proposed solutions are just too obvious.

He's done the work in javascript... well, embed Rhino (http://
www.mozilla.org/rhino) in the java app and he's all done.

(sorry, I couldn't refrain myself)
 
B

Bas

Full Javadocs, implement java.io.Serializable fully and correctly, use lots of
anonymous inner classes, annotations, NIO and java.util.concurrent, ...

That for sure won't work.

On top of those, XML embedded in SOAP, yeah, *then* it'll work.

Deployed as a SOA architecture but that goes without saying.
 
G

Gordon Beaton

The problem with that approach is not just your victim reads it. You
are in effect creating a Monty Python Hungarian phrase book.

Anyone who copies a (convoluted but correct) solution to a typical
homework problem from a Usenet post without understanding it is most
likely using it to solve a similar homework problem, and deserves the
same "benefits" as the original poster.

/gordon

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top