Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Validating a String in Java

Thread view: 
Ronan40060 - 12 Nov 2007 13:58 GMT
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
Gordon Beaton - 12 Nov 2007 14:20 GMT
> 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

--
Wojtek - 13 Nov 2007 18:55 GMT
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.

Signature

Wojtek :-)

Gordon Beaton - 13 Nov 2007 19:53 GMT
> 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.

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

/gordon

--
Roedy Green - 13 Nov 2007 22:09 GMT
>> 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.

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

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Gordon Beaton - 14 Nov 2007 07:03 GMT
>>> 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.
[quoted text clipped - 3 lines]
> 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

--
Lew - 14 Nov 2007 01:59 GMT
> Gordon Beaton wrote :
>> This sounds a lot like homework,
[quoted text clipped - 5 lines]
> 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, ...

Signature

Lew

Bas - 14 Nov 2007 04:52 GMT
> 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.
Chris ( Val ) - 12 Nov 2007 15:06 GMT
> 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.

--
Chris
Lew - 12 Nov 2007 15:33 GMT
Ronan40060 wrote:
>> 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

> 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).

Signature

Lew

Chris ( Val ) - 13 Nov 2007 05:16 GMT
> Ronan40060 wrote:
> >> typical steps would be
[quoted text clipped - 11 lines]
> 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.

--
Chris
Daniel Pitts - 12 Nov 2007 16:56 GMT
> Dear Friends
>
[quoted text clipped - 15 lines]
>
> 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.
Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Bas - 14 Nov 2007 04:50 GMT
On Nov 12, 5:56 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:

> 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)
Roedy Green - 13 Nov 2007 08:10 GMT
>populated from a text field , should contain only 0-9,A-Z,a-z,Space '
>',Hyphen '-',Full stop '.',Comma ',',Plus '+'

You can do that with a regex. See
http://mindprod.com/jgloss/regex.html
or a String.indexOf on each character in the string.
see http://mindprod.com/jgloss/string.html

You might look at com.mindprod.common11.StringTools.isLegal which
implements the indexOf technique.

see http://mindprod.com/products1.html#COMMON11
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.