Saturday, April 24, 2010

Randum String generate

public class RandomString {

public void getRamdomString()
{
java.util.Random r = new java.util.Random();
//System.out.print(r.nextInt());
int i = 1, n = 0;
char c;
String str="";
for (int t = 0; t < 5; t++) {
//number of alfabat characters added in front of String token
while (true) {
i = r.nextInt(10);
if (i > 5 && i < 10) {

if (i == 9) {
i = 90;
n = 90;
break;
}
if (i != 90) {
n = i * 10 + r.nextInt(10);
while (n < 65) {
n = i * 10 + r.nextInt(10);
}
}
break;
}
}
c=(char)n;

str= String.valueOf(c)+str;
}
while(true){
i = r.nextInt(100000);// number of integer padded at last
if(i>9999) // number of integer padded at last
{
break;
}
}
str=str+i;
System.out.println(str);
}

public static void main(String[] args) {

RandomString obj=new RandomString();
while(true)
{
obj.getRamdomString();
}

}


}

Friday, April 23, 2010

Sending mail with attachment from java code

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package success;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail
{
String mailhost = "smtp.gmail.com"; //it is googles mail host use proper mail host for proper server
String senderid = "senderid@gmail.com"; ///authentication mail id
String password = ""; //password
String smtpport = "587";

String messagetype = "text/plain";
SendMail( String to, String message,String subject,String Username)
{

Properties props = new Properties();
props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.port", smtpport);
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.localhost", "gamil.com");



Session s = Session.getInstance(props, null);
s.setDebug(true);

MimeMessage message1 = new MimeMessage(s);
try
{
InternetAddress from1 = new InternetAddress(senderid, Username);
InternetAddress to1 = new InternetAddress(to);

message1.setSentDate( new Date() );
message1.setFrom( from1 );
message1.addRecipient(javax.mail.Message.RecipientType.TO, to1);

message1.setSubject(subject);
message1.setContent(message, messagetype);

Transport tr = s.getTransport("smtp");
tr.connect(mailhost, senderid, password);
message1.saveChanges();
tr.sendMessage(message1, message1.getAllRecipients());
tr.close();

}
catch (Exception e)
{
System.out.println(e.toString());
}

}

SendMail( String to, String message,String subject,String Username, String[] attachments)
{

Properties props = new Properties();
props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.port", smtpport);
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.localhost", "gamil.com");



Session s = Session.getInstance(props, null);
s.setDebug(true);

MimeMessage message1 = new MimeMessage(s);
try
{
InternetAddress from1 = new InternetAddress(senderid, Username);
InternetAddress to1 = new InternetAddress(to);

message1.setSentDate( new Date() );
message1.setFrom( from1 );
message1.addRecipient(javax.mail.Message.RecipientType.TO, to1);

message1.setSubject(subject);

// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);

//use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();

//add the message body to the mime message
multipart.addBodyPart(messageBodyPart);

// add any file attachments to the message
addAtachments(attachments, multipart);

// Put all message parts in the message
message1.setContent(multipart);

// Send the message
Transport tr = s.getTransport("smtp");
tr.connect(mailhost, senderid, password);
message1.saveChanges();
tr.sendMessage(message1, message1.getAllRecipients());
tr.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}

}

protected void addAtachments(String[] attachments, Multipart multipart)
throws MessagingException, AddressException
{
for (int i = 0; i <= attachments.length - 1; i++) {
String filename = attachments[i];
MimeBodyPart attachmentBodyPart = new MimeBodyPart();

//use a JAF FileDataSource as it does MIME type detection
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));

//assume that the filename you want to send is the same as the
//actual file name - could alter this to remove the file path
attachmentBodyPart.setFileName(filename);

//add the attachment
multipart.addBodyPart(attachmentBodyPart);
}
}



public static void main(String[] args) {
// TODO code application logic here
//Text Mail Sending................
SendMail sm = new SendMail("reciverid@yahoo.co.in", "Java mail sending test", "test", "debmalya");


//Attachment mail sending................
String[] filenames = {"d:\\B2.JPG"};
SendMail sm1 = new SendMail("reciverid@gmail.com", "Java mail sending test", "test", "debmalya",filenames);
}
}

Tuesday, April 20, 2010

getvalue from servlet and set in page field using ajax

servelet side


String str = containt.trim().replaceAll(" ", "`");
str = str.replaceAll("\r\n", "~");



javascript side

document.getElementById("prtype").value=prtype.replace(/`/g,' ').replace(/~/g,'\n');

Wednesday, April 7, 2010

some javascript input field checking

function flatnoValidation(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;

if (unicode== 32)
{
return false; //if the key is the backspace key (which we should not allow)
}
else if (unicode== 96)
{
return false; //if the key is the backspace key (which we should not allow)
}
else if (unicode== 126)
{
return false; //if the key is the backspace key (which we should not allow)
}
else
{
return true;
}

}

function blockUsedSymbols(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;
if (unicode== 96)
{
return false; //if the key is the ` key (which we should not allow)
}
else if (unicode== 39)
{
return false; //if the key is the ' key (which we should not allow)
}
else if (unicode== 126)
{
return false; //if the key is the ~ key (which we should not allow)
}
else
{
return true;
}
}
function nameonly(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;

if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else if(unicode==32)
{
return true;//if the key is the space key (which we should allow)

}
else if(unicode<65||unicode>90&&unicode<97||unicode>122)
{
return false; //if not a letter
}
return true;
}

function floorNoValidation(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;
if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else if (unicode== 32)
{
return false;//if the key is the space key (which we should not allow)
}
else if(unicode<48||unicode>57)
{
return false;
}
return true;
}

function numberonly(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;
if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else if (unicode== 32)
{
return true;//if the key is the space key (which we should allow)
}
else if(unicode<48||unicode>57)
{
return false;
}
return true;
}
function passwordonly(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;
if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else if(unicode==95)
{
return true;//if the key is the _ underscore key (which we should allow)
}
else if (unicode==32)
{
return false;//if the key is the space key (which we should not allow)
}
else if(unicode<48||unicode>57&&unicode<65||unicode>90&&unicode<97||unicode>122)
{
return false;
}
return true;
}

function trimThis(value1)
{
value1= value1.replace(/^\s+|\s+$/, '');
return value1;
}

function emailcheck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){

return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

return false
}

if (str.indexOf(at,(lat+1))!=-1){

return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

return false
}

if (str.indexOf(dot,(lat+2))==-1){

return false
}

if (str.indexOf(" ")!=-1){

return false
}

return true
}

function validateTextAreaLen(e,txt,len)
{
var unicode = e.charCode? e.charCode : e.keyCode;
var addrs = txt.value;
if(addrs.toString().length >= len)
{
if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else
{
return false;
}
}
else
{
if (unicode== 96)
{
return false; //if the key is the ` key (which we should not allow)
}
else if (unicode== 39)
{
return false; //if the key is the ' key (which we should not allow)
}
else if (unicode== 126)
{
return false; //if the key is the ~ key (which we should not allow)
}
else
{
return true;
}

}
}

function onblurMonyonly(txtf)
{
if(trimThis(txtf.value).toString().length == 0)
{
txtf.value='0.00';
}
else if(txtf.value == '.')
{
txtf.value='0.00';
}
else if(txtf.value.toString().indexOf('.') != -1)
{
if(txtf.value.toString().indexOf('.') == txtf.value.toString().length-1)
{
txtf.value = txtf.value.toString()+'00';
}
else if(txtf.value.toString().indexOf('.') == txtf.value.toString().length-2)
{
txtf.value = txtf.value.toString()+'0';
}
}
else
{
txtf.value = txtf.value.toString()+'.00';
}

}

function resetMoneyOnly(txtf)
{
txtf.value = '';
}
function moneyonly(e,txtf)
{
var unicode=e.charCode? e.charCode : e.keyCode;
var st = txtf.value;
//alert(unicode)
if (unicode== 8)
{
return true;
//if the key isn't the backspace key (which we should allow)
}
else if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}

else if(unicode == 46)
{
if(st.toString().indexOf(".",0) != -1)
{
return false;
}

}
else if (unicode<48||unicode>57) //if not a number
{

return false //disable key press
}
else if(st.toString().indexOf(".",0) != -1)
{
if(st.toString().length - st.toString().indexOf(".",0) > 2)
{
return false;
}
}

return true;
}