validation in java
DESCRIPTION
this code wil help you to add validation in your java program...send ur feedback in [email protected]TRANSCRIPT
![Page 1: Validation in java](https://reader036.vdocuments.us/reader036/viewer/2022082701/5525a4214a7959ac488b4cc3/html5/thumbnails/1.jpg)
Validation in java
1. Name Validationimport java.io.*;public class Name{public boolean nameValidate(String name){char [] str = new char[name.length()];name.getChars(0, name.length(), str, 0);for(int i = 0; i<str.length; i++){if((Character.getNumericValue(str[i])<10 || Character.getNumericValue(str[i])>35)&& Character.isSpaceChar(str[i])==false){return false;} }return true;}
public static void main(String[] args)throws IOException { Name main = new Name();BufferedReader br=new BufferedReader(new InputStreamReader(System.in));System.out.println("Enter Name Note:does not enter special characters");String name=br.readLine();if(main.nameValidate(name)) System.out.println("valid");else System.out.println(" Not valid"); }}
![Page 2: Validation in java](https://reader036.vdocuments.us/reader036/viewer/2022082701/5525a4214a7959ac488b4cc3/html5/thumbnails/2.jpg)
2. Number Validationimport java.io.*;public class Number { public boolean containsOnlyNumbers(String str) { //It can't contain only numbers if it's null or empty... if (str == null || str.length() == 0) return false; for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) return false; } return true; }
public static void main(String[] args)throws IOException { Number main = new Number();BufferedReader br=new BufferedReader(new InputStreamReader(System.in));System.out.println("Enter Number");String name=br.readLine();if(main.containsOnlyNumbers(name)) System.out.println("");else System.out.println("plz enter number only"); }}
3. Date validationimport java.util.Date;import java.text.ParseException;
![Page 3: Validation in java](https://reader036.vdocuments.us/reader036/viewer/2022082701/5525a4214a7959ac488b4cc3/html5/thumbnails/3.jpg)
import java.text.DateFormat;import java.text.SimpleDateFormat;import java.io.*;public class DateValid{public boolean isValidDate(String date){ SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); Date testDate = null; try { testDate = sdf.parse(date); }
catch (ParseException e) {
return false; }
if (!sdf.format(testDate).equals(date)) { return false; }
return true;
} public static void main(String args[]) throws IOException{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));System.out.println("enter date");String s=br.readLine();DateValid dv = new DateValid();if(dv.isValidDate(s))System.out.println("true");elseSystem.out.println("false");
![Page 4: Validation in java](https://reader036.vdocuments.us/reader036/viewer/2022082701/5525a4214a7959ac488b4cc3/html5/thumbnails/4.jpg)
}}
4. Email Validation
import java.util.regex.*;
import java.io.*;
public class EmailValidation
{
public static void main(String []args) throws IOException
{
String email;
System.out.println("enter EmailId");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
email=br.readLine();
Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");
Matcher m=p.matcher(email);
boolean b=m.matches();
if(b==true)
{
System.out.println("Valid Email ID");
}
else
{
System.out.println("InValid Email ID");
![Page 5: Validation in java](https://reader036.vdocuments.us/reader036/viewer/2022082701/5525a4214a7959ac488b4cc3/html5/thumbnails/5.jpg)
}
}
}
5. Special Character validation
import java.io.*;
public class NameNumber
{
public boolean containsOnlyNumbers(String str) {
//It can't contain only numbers if it's null or empty...
if (str == null || str.length() == 0)
return false;
for (int i = 0; i < str.length(); i++)
{
if (Character.isLetterOrDigit(str.charAt(i)))
return false;
}
return true;
}
public static void main(String[] args)throws IOException
{
NameNumber main = new NameNumber();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter alphanumeric String");
![Page 6: Validation in java](https://reader036.vdocuments.us/reader036/viewer/2022082701/5525a4214a7959ac488b4cc3/html5/thumbnails/6.jpg)
String name=br.readLine();
if(main.containsOnlyNumbers(name))
System.out.println("Not valid");
else
System.out.println("valid");
}
}