-1

Is there a possible way to do that?

e.g In my application form a have a field for the user's name which is a String. But how can I disallow the user from giving for example "L3bron James" instead of giving "Lebron James"?

String sql_name = name.getText();
// name is a JTextField
if (sql_name.length() == 0)
    {
        //how to check if the name does not contain int digits
    JOptionPane.showMessageDialog(null,"Name is a string ,NOT an Integer, not a null value!!","Invalid Name",JOptionPane.ERROR_MESSAGE);
    }
5
  • You can control it programatically in runtime. If input is a number, delete this char from form field. Commented Aug 7, 2014 at 12:01
  • possible duplicate of Checking each character for a number Commented Aug 7, 2014 at 12:01
  • There are key event listeners. Commented Aug 7, 2014 at 12:02
  • And document event listeners. Commented Aug 7, 2014 at 12:03
  • Be aware that there are some valid names that contain an integer. For example see Men's Names and Titles: When a man is named after his father who is a "Jr.," he is called "the third," once written with either the numeric 3rd or the Roman numeral III but now confined almost exclusively to the latter. Commented Aug 7, 2014 at 12:28

4 Answers 4

3

You can use a regular expression on the string to see if it contains number characters:

if (Pattern.compile("[0-9]").matcher(sql_name).find()) {
    // Show error message
}
Sign up to request clarification or add additional context in comments.

3 Comments

does Pattern need an import?
I mean it compiled but still the 2014 for string was accepted when it shouldn't
Could you show the code you used? The code above should work. Just verified that it gives the expected results here.
0

It depends on where you want to filter for integers. Following this answer How to filter string for unwanted characters using regex? you can use regex to check for integers and prompt the user again if you found an integer. Or you could just replace them which would be kind of rude.

Comments

0

You can use StringUtils.isAlphaSpace from apache commons

StringUtils.isAlphaSpace(null)   = false
StringUtils.isAlphaSpace("")     = true
StringUtils.isAlphaSpace("  ")   = true
StringUtils.isAlphaSpace("abc")  = true
StringUtils.isAlphaSpace("ab c") = true
StringUtils.isAlphaSpace("ab2c") = false
StringUtils.isAlphaSpace("ab-c") = false

Comments

0

Alternative Solution

boolean flag = false;
for (int i = 0; i < sql_name.length(); i++) {
        if (Character.isDigit(sql_name.charAt(i)))
                flag = true;// number exists in the string
            }
if (flag == true)
   JOptionPane.showMessageDialog(null,"Name is a string ,NOT an Integer, NOT a null value!!","Invalid Name",JOptionPane.ERROR_MESSAGE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.