aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Postal Code Validation Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Postal Code Validation Issue" Watch "Postal Code Validation Issue" New topic
Author

Postal Code Validation Issue

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I am doing Postal code validation. The postal code can take 7 Alphanumerics inclucluding one blank. Now when I enter the postal code without any space(A1ED3R) or it will create an empty space in the middle(A1E D3R) which is correct. But if I put a space anywhere else like(A 1ED3R) it is showing me exactly like this(A 1ED3R) after validation. I want to put only one space in the middle like(A1E D3R) no matter where I put the space.

I am using substring for the validation here. I would appreciate if someone can help me. Thanks!!!

Here is my code:

Rose Rimsky
Greenhorn

Joined: Feb 10, 2010
Posts: 25
Hi Sam, I think this problem is because the condition its not being fulfill
You're only checking if the length of the postal code is equal to 6, but when you enter a space as you mentioned:
A 1ED3R
the length isn't 6, it's 7, so the condition isn't true.

Hope this solve your problem! If not, then we can figure out another solution.


Fairy tales are more than true: not because they tell us that dragons exist, but because they tell us that dragons can be beaten. – G.K. Chesterton
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Sam,
Are you aware of regular expressions. They let you express patterns in a very compact way. So you could say you want one "whitespace or alphanumeric character" followed by 5 alphanumeric followed by one whitespace/alphanumeric. Or similar. Which isn't your pattern of course.

Of you could get rid of the blank and add it in at the end.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I changed the condition to if (postalCode.length == 6)

Now I am still getting wherever I put the space and another space in the middle and the last letter is getting disappeared. So if the enter (A 1ED3R) the result I am getting is (A 1E D3). Here is the changed code.

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Of course the last letter will disappear because you are only looking at a certain range. Adding that extra letter will bump the part you want out.

Remove the spaces and format your string or just ignore it and do it all on the server.

Eric
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
But I am getting two white spaces. I am just wondering if somehow I can replace the white spaces from the substrings to display it correctly.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Yes you can a simple string replace will remove the whitespaces. Do it before you format the string.

Eric
 
 
subject: Postal Code Validation Issue