Created
February 23, 2012 11:46
-
-
Save sangramanand/1892516 to your computer and use it in GitHub Desktop.
Validating phone number using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* | |
* @author anand | |
*/ | |
public class ValidatePhoneNumber { | |
public static void main(String[] argv) { | |
String phoneNumber = "1-(80..2)-321-0361"; | |
System.out.println(phoneNumber.length()); | |
String regex = "^\\+?[0-9. ()-]{10,25}$"; | |
Pattern pattern = Pattern.compile(regex); | |
Matcher matcher = pattern.matcher(phoneNumber); | |
if (matcher.matches()) { | |
System.out.println("Phone Number Valid"); | |
} else { | |
System.out.println("Phone Number must be in the form XXX-XXXXXXX"); | |
} | |
} | |
} |
@ainie89
you can achieve this by using this simple logic.
if( ! textField2.getText().toString().matches(textField1))
{
// do this..
}
hope it helps.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi.
Need help here.
I have 2 input text where user must enter their phone number. Then if the phone number is match, it will go to other page, if not the user must re-enter the phone number. The problem is, how to compare the value that entered by the user and verify whether it is match or not?