Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Regular Expressions

Have Programming Question? Ask it here!
 
 
 


Janet
Regular Expressions
1 Answer(s)      3 years and 9 months ago
Posted in : Java Beginners


hi tried writing this program and got stuck give some help please


Write a java program to accept name, phone number , address,email and validate them.
Name should Start with upper case and the lower cases, phone number Should start with +256-041-369024.


Thanx for yo help.

Advertisement
View Answers

July 22, 2010 at 4:03 PM


Hi Friend,

Try the following code:

import java.util.*;
import java.util.regex.*;
class RegularExpressions{

public static boolean validateName( String name ) {
return name.matches( "\\p{Upper}(\\p{Lower}+\\s?)" );
}
public static boolean validateAddress( String address )
{
return address.matches( "\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );

}
public static boolean validatePhone( String phone )
{
return phone.matches( "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{6})$" );
}
public static boolean isEmailValid(String email){
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if(matcher.matches()){
isValid = true;
}
return isValid;
}


public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter Name: ");
String name=input.nextLine();
while(validateName(name)!=true){
System.out.print("Invalid Name!");
System.out.println();
System.out.print("Enter Name: ");
name=input.nextLine();
}

System.out.print("Enter Phone Number: ");
String phone=input.nextLine();
while(validatePhone(phone)!=true){
System.out.print("Invalid PhoneNumber!");
System.out.println();
System.out.print("Enter Phone Number: ");
phone=input.nextLine();
}

System.out.print("Enter Address: ");
String address=input.nextLine();
while(validateAddress(address)!=true){
System.out.print("Invalid Address!");
System.out.println();
System.out.print("Enter Address: ");
address=input.nextLine();
}

System.out.print("Enter EmailId: ");
String email=input.nextLine();
while(isEmailValid(email)!=true){
System.out.print("Invalid Email!");
System.out.println();
System.out.print("Enter EmailID: ");
email=input.nextLine();
}

}
}

Thanks


Related Tutorials/Questions & Answers:
Regular expressions
Regular expressions  Sir/Madam, please tell me the meaning of the following code import java.io.*; import java.util.*; class Demo{ public static void main(String args[]){ double d=1000000.1232323d
Regarding Regular Expressions
Regarding Regular Expressions  Hi I would like to supply my regular..., it will return the default value as i have mentioned in the Regular Expressions. Please acknowledge me as soon as possible. Thanks, K.ManojKumar.
Advertisements
Regular Expressions
Java: Regular Expressions Regular expressions are the most common..., Regular expressions are neither regular nor expressions. And Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have
Regular Expressions - Java Beginners
Regular Expressions  hi tried writing this program and got stuck give some help please Write a java program to accept name, phone number , address,email and validate them. Name should Start with upper case and the lower
Java Validations using Regular Expressions
Java Validations using Regular Expressions In this section, you will learn how to validate different fields using regular expressions. Here we have created the methods to validate the name,address, email and phone number entered
How to use regular expressions - Java Beginners
How to use regular expressions  how do i put a label and text field on a frame  Hi Friend, Here i am sending you full source code in Java Swing to put label and text field on a frame. // LabelTextSwing.java
Matching Address using regular expressions
Matching Address using regular expressions  ... using regular expression.For this we are going to make program named... the resulting regular expression to match from 2 to 3 repetitions of the preceding
i want validation for one text field and text area,email,combobox, in that email will validte by useing regular expressions?
i want validation for one text field and text area,email,combobox, in that email will validte by useing regular expressions?  i want validation... regular expressions?   <html> <h2>Form Validation<
Regular Expressions
Find.java.    Matching Address using regular expressions... Regular Expressions      ... Regularexpression.    Print Matchingwords using Regular expression
Expressions
Java NotesExpressions Expressions are the basic way to create values. Expressions are created by combining literals (constants), variables, and method... are used to combine literals, variables, methods calls, and other expressions
Boolean Expressions
Boolean Expressions Name ______________________ Assume the following: int k = 10; int j = 6; boolean b = true; Show the value (true or false) of each of the following expressions. __________ 1 > 2 __________ b __________ k
Boolean Expressions
Boolean Expressions Name ______________________ Assume the following: int k = 10; int j = 6; boolean b = true; Show what is printed by each of the following statements. __________ System.out.println( 1 > 2
Identify correct and incorrect conditional expressions, BETWEEN expressions, IN expressions, LIKE expressions, and comparison expressions.
Identify correct and incorrect conditional expressions, BETWEEN expressions, IN expressions, LIKE expressions, and comparison expressions. Prev Chapter 9. EJB-QL Next   
Regular expression
Regular expression  how i write a regular expression for a pattern which present in between
String Expressions
String Expressions Name ______________________ Assume the following: String a = "abc"; String h = "Hello"; String name = "Michael Maus"; Show what is printed by each of these statements. __________ System.out.println( h.length
Applying Regular Expressions on the Contents of a File
Applying Regular Expressions on the Contents of a File       This section illustrates you how to search a string in a file content. Following takes the file name from
Summary - Expressions
Java: Summary - Expressions Parentheses () have three uses: Grouping to control order of evaluation, or for clarity. Eg, (a + b) * (c - d) After a method name to enclose parameters. Eg, x = sum(a, b); Around a type name
Regular expression
Regular expression  can some one explain how below regex works, ndmurl1=${ndmurl#${ndmurl
Form Validation using Regular Expressions is JavaScript
Form Validation using Regular Expressions is JavaScript... is JavaScript validation using Regular Expressions? The JavaScript Validating... incorporated regular expressions. This article I will present a brief tutorial
Regularexpressions Tutorial
Regular Expressions Introduction In this tutorial I will show you how to use Regular Expressions in Java. What is Regular Expressions? A regular expression... are Regular Expressions examples: Split string after each character using regular
Regular expression
Regular expression  I have the regular expression : var x=/(^\d+$)/ which allows only numbers. It does not accept character, decimals. However it accepts zero. I want an expression that takes numbers(no decimals)except zero
Regular expression
Regular expression  how i extract string Coalition of civil society organizations and CSOs from string:- CSOs : What happening in Yemen is political crisis SANA'A, May 16 (Saba)- Coalition of civil society organizations (CSOs
java tree expressions - XML
java tree expressions  hi all, i have a problem in tree expressions. see there is a document tree like a parent document has no. of child documents. the problem is whenever i click on + sign it means it will have surely have
JSPs : Expressions
JSPs : Expressions This tutorial contains description of Expressions which is one of JSP page element. Expressions : Expressions in JSPs is used... a JSP expression but for same, semicolon is used for scriptlets. Expressions
javascript regular expression
javascript regular expression  i need a regular expression code for JavaScript
A regular expression contains another regular expression - PHP
A regular expression contains another regular expression   Please explain how it works in PHP... RegEX1 = "a.*b"; RegEx2 = "a1.*b"; Thanks
PHP Regular Expression
Regular Expressions PERL Style Regular Expressions PHP Examples PHP GET...PHP regular expression allows programmer to perform various task like comparing, finding and replacing a particular type of strings. Using Regular
A program in Java to evaluate APL expressions.
A program in Java to evaluate APL expressions.  APL is a programming language in which arithmetic/mathematical expressions are evaluated right... in Java to evaluate APL expressions. (Hint the expression is read as a string
split string with regular expression
split string with regular expression  How to split string with regular expression in Java
regular expression for special characters
regular expression for special characters  How to find out regular expression for special characters
using regular expression
using regular expression  How can we extract string 'abc.com ' from a string http://info@abc.com using regular expression of php
regular expression for phone number
regular expression for phone number  Hi, i need the regular expression code for phone number in PHP. Thanks.   PHP - regular expression code for phone number \+?([0-9]{2})-?([0-9]{3})-?([0-9]{6,7
java regular expression
java regular expression  hi i need java expression which allows all characters with minimum 2 characters and maximum 20 characters in answers field after selecting security questions
Regular expression in c++{MFC}
Regular expression in c++{MFC}  Hi ALL, I want to build a regular expression in c++{MFC} which validates the URL. The regular expression must... of %5C & %2F character. How can we develop a generic Regular Expression
Regular expression contains character - PHP
Regular expression contains character  A PHP script that can check if the regular expression contains character
PHPMYSQL REGULAR EXPRESSION - WebSevices
PHPMYSQL REGULAR EXPRESSION  Hello all, I have to read a file and insert the match into mysql.File has a follwing lines. user196 Auth-Type := Local, User-Password == mypass@wd Session-Timeout = 3600
Regular expression in PHP for email - PHP
Regular expression in PHP for email  can any one tell me how to write regular expression in PHP for email to retrieve all the valid email ID's from the email
URL Regular Expression for Struts Validator
URL Regular Expression for Struts Validator  I want to know how to validate a url link in struts validation.xml. Please share me the regular expression for url link validation
URL Regular Expression for Struts Validator
URL Regular Expression for Struts Validator  I want to know how to validate a url link in struts validation.xml. Please share me the regular expression for url link validation
QuickREx
; Regular Expressions are powerful but can be confusing at times. I regularly found myself working with scrapbook-pages to test my regular expressions... and to be able to play with regular expressions more easily. QuickREx gives you a view
Expressions, Statements, and Blocks in java 7
In this section we will discuss expressions, Statements and Blocks in java 7. This is one type of Language Fundamentals
String Regex Methods
Java: String Regex Methods In addition the the Pattern and Matcher classes, there is some support for regular expressions in the String class.... // Example of splitting lines from input file with regular expression. while
Regular Expression for removing XML Tags
Regular Expression for removing XML Tags  Hi, Good Afternoon.I want a Regular expression in java which is removing the XML Tags in the specified... after applying regular expression is only "How are you".Need to remove all XML
about regular expression - Java Beginners
about regular expression  Consider the grammar G: S -> ( L ) | a L -> L, S | S a) Eliminate the left-recursion from grammar G. b) Construct a recursive descent parser for G
What is regular expression beginning of line ?
What is regular expression beginning of line ?  Hi, I one of my Java program I have to write a program to find the beginning of a line and then replace the text using replaceAll() function of String. What is regular expression
including index in java regular expression
including index in java regular expression  Hi, I am using java regular expression to merge using underscore consecutive capatalized words e.g.... from you is that is it possible using regular expression to avoid first word
Regular Expression Help Help HELP!!!!
Regular Expression Help Help HELP!!!!  HI all I am very new to java, but i have this problem i got a string e.g Courses['07001'].Title = "International Students 1"; So i need to extract only the "international Students", i
regular expression expalanation i need
regular expression expalanation i need  hi......... here is my code could explain it <html> <script language = "Javascript">... your regular expression. ^ shows beginning of string
Regular Expression Search Program
Regular Expression Search Program       Regular Expression: Regular Expression is used to perform many... of operations like search, edit and manipulating string can be performed by the regular
Regarding regular expression with swing - Swing AWT
Regarding regular expression with swing  Regular Expression for value that should be greater than 0.0 and less than 999.9 0.0 > value < 999.9 Please Check it, in cell editor in JTable to validate a particular
Advertisements
 

 

 

DMCA.com