0

I have a name field and I have to validate it against a regex which is supposed to accept only alphabets and spaces. Can I get a proper running code for the same.

I basically need to check if the string matches the pattern.

So if I enter "rahul bose" it should return true but if i enter "rahul-bose" or "rahul bose 9" it should return false.

I tried something like this but it fails always

package pckg;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextRegex {
    private String name;

    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }

    public boolean checkError(String name){
        String regex = "[A-Za-z]";
        if(Pattern.matches(regex, name))
            return true;
        return false;

    }
    public static void main(String[] arg){
        TextRegex textRegex = new TextRegex();
        textRegex.setName("nayanava");
        System.out.println(textRegex.checkError(textRegex.getName()));
    }
}
brimborium
  • 8,880
  • 9
  • 46
  • 73
Nayanava
  • 21
  • 3

0 Answers0