Suppose your requirement is to validate a textfield in which user provides a email id, here is a simple example on how to do it.
Consider the following requirement for your field validation.
1) First 4 characters should be numbers
2) Next set of characters until reaching a "@" should be alphabets, followed by the domain as say - gmail.com or yahoo.com etc...
Here is the code to validate it.
public class ValidateEmailField
{
public static void main(String[] args)
{
String inputString = "1234rajacasssss@gmail.com";
Pattern pattern=Pattern.compile("(^[0-9]{4}[A-Za-z]+)@[a-z]+\\.[a-z]+");
Matcher m=pattern.matcher(inputString);
System.out.println(inputString);
if (!m.matches())
{
System.out.println("Invalid Email Id entered");
}
else
System.out.println("Email Id is valid.");
}
You can give different values for input string and verify.
Consider the following requirement for your field validation.
1) First 4 characters should be numbers
2) Next set of characters until reaching a "@" should be alphabets, followed by the domain as say - gmail.com or yahoo.com etc...
Here is the code to validate it.
public class ValidateEmailField
{
public static void main(String[] args)
{
String inputString = "1234rajacasssss@gmail.com";
Pattern pattern=Pattern.compile("(^[0-9]{4}[A-Za-z]+)@[a-z]+\\.[a-z]+");
Matcher m=pattern.matcher(inputString);
System.out.println(inputString);
if (!m.matches())
{
System.out.println("Invalid Email Id entered");
}
else
System.out.println("Email Id is valid.");
}
You can give different values for input string and verify.