Task: Writea programt lid inputs using regular expressions. What will the applic
ID: 3917051 • Letter: T
Question
Task: Writea programt lid inputs using regular expressions. What will the application do? The program will validate different kinds of input. . Build Specifications 1. Write a method that will validate names. Names can only have alphabets, they should start with a capital letter, and they have a maximum length of 30 2. Write a method that will validate emails. An email should be in the following format: (combination of alphanumeric characters, with a length between 5 and 30, and there are no special characters)@(combination of alphanumeric characters, with a length between 5 and 10, and there is no special characters domain can be combination of alphanumeric characters with a length of two or three 3. Write a method that will validate phone numbers. A phone number should be in the following format: (area code of 3 digits) - (3digits) - 14 digits) 4. Write a method that will validate date based on the following format:Explanation / Answer
import java.util.regex.Pattern;
public class Main {
public static boolean isName(String s) {
if(Pattern.matches("[A-Z][a-z]{0,29}",s)) {
return true;
}
return false;
}
public static boolean isEmail(String s) {
if(Pattern.matches("[A-Za-z0-9]{5,30}@[A-Za-z0-9]{5,10}.[A-Za-z0-9]{2,3}",s)) {
return true;
}
return false;
}
public static boolean isPhone(String s) {
if(Pattern.matches("[0-9]{3}-[0-9]{3}-[0-9]{4}",s)) {
return true;
}
return false;
}
public static boolean isDate(String s) {
if(Pattern.matches("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/([0-9]{4})",s)) {
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println(isEmail("Abcde@gil.com"));
System.out.println(isEmail("Abcd1!ea@gil.com"));
System.out.println(isEmail("adr@gail.com"));
System.out.println(isEmail("Adadas@g$mail.com"));
System.out.println(isEmail("a"));
System.out.println(isPhone("942-521-5478"));
System.out.println(isPhone("98a-548a-585"));
System.out.println(isPhone(""));
System.out.println(isDate("31/12/0998"));
System.out.println(isDate("29/02/1998"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.