This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Postal Code Validation Issue

 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can a simple string replace will remove the whitespaces. Do it before you format the string.

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic