473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

String Validation in Java

In Java I want to validate a account number for a bank which will be the input be the use.It should have the following fomat:
1)It should start with SB
2)After SB it should be followed by number 2011
3)followed by a 4 digit number.
4)It should not be greater than 10 digit
eg:SB20115789
Oct 11 '11 #1

✓ answered by Frinavale

So, apparently in Java you need to declare a Pattern type based on the string that contains the regular expression.

You have to "compile" this Pattern in order to use it with a Matcher. The Matcher is the thing that will compare the String to the Regular Expression pattern.

So, you need to declare a Pattern based on a string that contains regular expression pattern that you want to check. And you need to "compile" that Pattern. Then you need to declare a new Matcher based on the compiled Pattern.

Like this:
Expand|Select|Wrap|Line Numbers
  1.  Pattern p = Pattern.compile("^SB2011[0-9]{4}$");
  2.  Matcher m = p.matcher(str); //str is the String that needs validating
Now you can call the Matcher's "matches()" method to see if the string you want to validate "matches" the pattern that you are expecting:

Expand|Select|Wrap|Line Numbers
  1. public boolean ValidateAccountNumber(string str)
  2. {
  3.  Pattern p = Pattern.compile("^SB2011[0-9]{4}$");
  4.  Matcher m = p.matcher(str); //str is the String that needs validating
  5.  return m.matches();
  6. }

Pretty clean isn't it :)

-Frinny

6 7611
Frinavale
9,735 Expert Mod 8TB
I recommend that you use Regular Expressions to validate the string containing the account number.

-Frinny
Oct 11 '11 #2
What do u mean by regular expressions?
Please explain.
Oct 11 '11 #3
Frinavale
9,735 Expert Mod 8TB
"Regular Expressions (wiki link)" are used for string comparisons in programming. It uses a "formal language" (special type of syntax) to describe a "pattern" that you want to compare your string to.

In your case, you have a pattern that you want to check:
  1. The string should start with SB
  2. After SB the string should contain the number "2011"
  3. Followed by a 4 digit number.
  4. The string should not be greater than 10 digit

What you need to do is translate this pattern into the Regular Expression syntax. That way you can use a Regular Expression to check if the String matches the pattern you described. If it matches then the string is valid, otherwise it's not valid.

The topic is quite large and the syntax varies slightly between programming languages.

Please refer to the Java Documentation on Regular Expressions to learn about how to use them in your application.

-Frinny
Oct 11 '11 #4
Frinavale
9,735 Expert Mod 8TB
In Regular Expression Syntax, when you want to check if a string "starts with" something you use the ^ character at the beginning of the string.

In your case you want to check if the String starts with "SB2011". So, to translate this into Regular Expression syntax you would have "^SB2011".

Now you need 4 digits to follow this. There are a couple of ways to do this but I think I'll demonstrate how to do this using a range.

A range is declared inside square brackets ([...]). If you want a lower case character between "a" and "z" then your range would be "[a-z]", if you want an upper case character between "A" and "Z" the range would be "[A-Z]". If you want a number that falls in the range between 0 - 9 the regular expression syntax would be "[0-9]".

So, now you know how to check for a digit...we can add this to your Regular Expression. The following will check if the string starts with "SB2011" and checks if this is followed by a single digit:
"^SB2011[0-9]"

But you need to check if 4 digits follow the "SB2011"...
You could insert 4 ranges from 0-9 like this:

"^SB2011[0-9][0-9][0-9][0-9]"

Or you could use the repetition syntax in Regular Expressions. If you want to check if something repeats you use curly braces followed by the number of times that that something repeats. For example if you want to check if a digit repeats 4 times you would have your range followed by "{4}".

In your case you would have:
"^SB2011[0-9]{4}"

I'm not too sure how you would check the length of a String using Regular Expressions...it's been a while since I've seriously used them.

But if your string can only contain "SB2011####" then you can check to see if the string "ends with" something using the $ character in your regular expression.
So, "^SB2011[0-9]{4}$" will match a string that only contains "SB2011####" (from start to end). The string cannot contain anything else.

-Frinny
Oct 11 '11 #5
Frinavale
9,735 Expert Mod 8TB
So, apparently in Java you need to declare a Pattern type based on the string that contains the regular expression.

You have to "compile" this Pattern in order to use it with a Matcher. The Matcher is the thing that will compare the String to the Regular Expression pattern.

So, you need to declare a Pattern based on a string that contains regular expression pattern that you want to check. And you need to "compile" that Pattern. Then you need to declare a new Matcher based on the compiled Pattern.

Like this:
Expand|Select|Wrap|Line Numbers
  1.  Pattern p = Pattern.compile("^SB2011[0-9]{4}$");
  2.  Matcher m = p.matcher(str); //str is the String that needs validating
Now you can call the Matcher's "matches()" method to see if the string you want to validate "matches" the pattern that you are expecting:

Expand|Select|Wrap|Line Numbers
  1. public boolean ValidateAccountNumber(string str)
  2. {
  3.  Pattern p = Pattern.compile("^SB2011[0-9]{4}$");
  4.  Matcher m = p.matcher(str); //str is the String that needs validating
  5.  return m.matches();
  6. }

Pretty clean isn't it :)

-Frinny
Oct 11 '11 #6
Tassos Souris
152 100+
One little note to it... you can make the pattern static member... in this way you avoid compiling the pattern every time you call the ValidateAccountMember() method...
Oct 15 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dan | last post by:
Hello. I have recently tried upgrading the MySql connector for my servlet from 2.0.4 to 3.0.9. I have found a minor limitation in 2.0.4 and was hoping that 3.0.9 would fix it. However, now I...
11
by: Ray Godfrey | last post by:
Hi there, Just a simple question, I think. I'm writing a program that takes information from a text file and uses that info to query a DB. Sounds simple enough..... The data would look...
1
by: Samuel | last post by:
Hello, I am looking for a way to check whether a string contains only word characters and a single space (!= any whitespace char), *regardless of the current locale*. In other words, any...
8
by: jake1138 | last post by:
Maybe this is a newbie thing and everyone already knows how to do this, but I figured I'd post these functions anyway in case someone finds them useful. I used Jack Klein's example (see link...
1
by: R.A. | last post by:
Hi I have the following method: public string FindNameList (string XmlStore) I have a dataset based on xml schema. I have created the schema in vs editor and created the dataset based on...
1
by: OriginalBrownster | last post by:
Hi there: I just had a quick thought of something that might be useful to me when creating my web app and I wanted some help on the idea; since this group has been helpful in the past. in my...
0
by: seyad | last post by:
hai i should allow user to enter a numbers and special charecters into my text box.give me a solution in java script chears seyad
14
by: prasath03 | last post by:
Dear All, I have one doubt for how to insert the java.sql.Date into ms-sql server. I tried the below code but it couldn't be inserted. I did wrong in my code that is ps.setDate(1,full_date_time)...
3
by: slg | last post by:
How can i validate the characters in a string are all hex chars. I tried following but it does not work. Regex r = new Regex(@"^(||)*"); TIA.
1
by: eldadno1 | last post by:
I'm actually a Java programmer, and I'm messing with JNI a bit, so I've managed to pass an int from java to c++ and to c# and back. no problems cause it gets serialized automatically. but when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.