Friday, June 22, 2012

Input validations and utility functions using Java Script

I would like to share input validations and javascript utility functins through this post.

1. Date validation using java script

function isValidDate(date){


   if( date.match( /^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/ )) {
       return true;
   }else{
      return false;
  }
}
This date validation almost covers all the date formats. please let me know if you need validation for other formats . I will prepare it and post it here.

2. SSN validation
  var matchArr = ssnIT.match(/^(\d{3})-?\d{2}-?\d{4}$/)
  var numDashes = ssnIT.split('-').length - 1;


  if (matchArr == null || numDashes == 1) {
    alert("Social Security Number must only contain values in NNN-NN-NNNN or NNNNNNNNN formats.")
} else if (parseInt(matchArr[1],10)==0) {
    alert("Invalid SSN: SSN's can't start with 000");
}

This is very useful validation. you can validate SSN on the UI using above code snippet. Let me know if
it helps you.
3. only alpha characters validations, no numeric characters

var regex = /^[A-z]+$/
if (!regex.test(Name)) {
      alert("input must only contain letters"};
}

Only alpha characters are validated using above function.
4. claculating Age using Javascript
function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
       age--;
    }
    return age;
}

There was requirement to validate the age of the user. I used the above function.
Thats all for now. I will post other useful validations and usefule scripts on this post.. Keep watching..!!

Thursday, May 31, 2012

Solution to Struts - DispatchAction Problem

We are using struts in our project. We are extending all our action classes to DispatchActionSupport. It enables a user to collect related functions into a single Action and call them depending on the parameter name passed along with request parameters.

This is really a good struts feature which allows us to have modular classes instead of having only one method in Action class and different Action classes for each request.

But this feature has one limitation. Sometimes it doesn’t invoke the method based on the action parameter and it throws the following error message.

Request[/atsTool/atsPartnerPermissions] does not contain handler parameter named 'action'. This may be caused by whitespace in the label text

Reproducible steps:
  1.  The user fills the JSP form as usual;
  2.  The user clicks on the submit button in order to retrieve or delete or modify;
  3.  The user get the following error message : Request[/atsTool/atsPartnerPermissions] does not contain handler parameter named 'action'. This may be caused by whitespace in the label text
This is very strange and intermittent problem.
In DispatchAction , struts first retrieves the request parameter and then invokes the method with name as parameter value.

 But here when above erroneous situation occurs. Struts retrieves the request parameter value but cannot invoke the method. I still don’t know why it is unable to invoke the method even though it has the request parameter value.

There is only one solution to this strange problem. DispatchAction class have one more method with following signature.

protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

Whoever is using DispatchAction class and facing above problem has to override the unspecified method. Then there you can retrieve the parameter value and based on the parameter value we can invoke the respective method.

I hope the above information will be helpful.

keep following..!!

Monday, April 26, 2010

Search Plugin : Firefox

IT folk's frequent task is to track his or his subordinate's issues. Every issue has a unique id. Usually we search it in Issue tracking system like bugzilla, Jira. Opening specific issue tracking site, specify issue id to search is a very meticulous and irritating task. It takes a most of the useful time.

You can simplify this task by using Google search plug-in for Firefox. If you have installed Mozilla Firefox, go the directory $MOZILLA_HOME/searchplugins. You will find search plug-ins for amazondotcom , answers, eBay and yahoo.

You can create similar search plug-in for your issue tracking system. Follow the below steps to create your own plug-in.

• Install Firefox and go to $MOZILLA_HOME/searchplugins directory
• Copy one of the xml file and copy it as your plug-in name e.g.: openwave.xml
• Open it and customize it according to your specifications.
• Change ShortName, Description,
• Template site and its parameter
template="http://its.openwave.com/ShowSingleIssue.ASP?" // Issue tracking site
Param name="IssID" value="{searchTerms}" // search keyword, e.g issue ID

• SearchForm is to mention a site name to open when user gives blank search key.
• Add the image by specifying the image data of type image/x-icon
• Restart The Firefox
• See the appearance in Google search plug-in.





Enjoy this plug-in and save the time. Let me know if you face any problems while creating and configuring your own search plug-in.

Wednesday, February 24, 2010

Go Green : PseudoPing

Persistent internally follows Green Persistent movement to save the energy. We have seen tremendous success in this movement. As a part of the movement, all the employees have to switch off their machines while leaving office. Admin people cheks all the machines one by one to find out whether any machine is alive. There are around 5K machines, so it's meticulous task to go through all the machines.

Just a thought of PseudoPing came in mind that why dont we ping all the machines and list out only alive machines and send a mail to the owner that his machine was on at particular time. It's small program but it saves manual work, time plus energy.

PseudoPing is simple program with one java class to represent Machine object, one text file containing all the machine names, owners and his mail-id, one class to read the hosts text file and one java class for the functionality.

PseudoPing.java :

package com.pspl;
import java.io.*;
import java.util.*;
/**
* This class is used to retreive all the machine names from file
* and list out all the alive machine in the network
* @author Rajiv Karambalkar
*/
public class PseudoPing {
public static void main(String[] args) {
Resource resource = new Resource();
ArrayList machines = resource.readHosts();
for (Machine machine : machines) {
String ip = machine.getMachineName();
String pingCmd = "ping " + ip;
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);
BufferedReader in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.indexOf("Ping request could not find host") != -1) {
System.out.println("Machine is not Alive : " + ip);
break;
} else if (inputLine.indexOf("Reply from") != -1) {
System.out.println("Machine is Alive : " + ip);
break;
}
}
in.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}

Machine.java

package com.pspl;
/**
* This class represents Machine object
*
* @author Rajiv Karambalkar
*/
public class Machine {

String machineName,emailId,location,machineOwner;

public Machine(String machineName, String machineOwner, String location,
String emailId) {
this.machineName = machineName;
this.location = location;
this.machineOwner = machineOwner;
this.emailId = emailId;
}
public String getMachineName() {
return machineName;
}
public void setMachineName(String machineName) {
this.machineName = machineName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getMachineOwner() {
return machineOwner;
}
public void setMachineOwner(String machineOwner) {
this.machineOwner = machineOwner;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}

Resource.java
package com.pspl;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

/**
* This class is used to read the hosts file
*
* @author Rajiv Karambalkar
*/
public class Resource {
File file = new File("D:\\persistent.hosts");
public ArrayList readHosts() {

File file = new File("D:\\persistent.hosts");
FileInputStream fis = null; BufferedInputStream bis = null;
DataInputStream dis = null; String[] readData = null;
ArrayList machines = new ArrayList();

try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
readData = dis.readLine().split(":");
if (readData.length < 4)
System.out
.println("Invalid Entry : Please Specify Machine Name:Machine Owner: Location : Email ID :-->"
+ readData.toString());
Machine machine = new Machine(readData[0], readData[1],
readData[2], readData[3]);
machines.add(machine);
}
// dispose all the resources after using them.
fis.close();bis.close();dis.close();
} catch (Exception e) {
e.printStackTrace();
}
return machines;
}
}

and sample hosts file :

ps5088.domainname.co.in:5B C11:Mahesh Patil: mahesh_patil@domainname.co.in

You can customize the java files according to your requirements. Mailing part is remaining but you can add it.

Go Green.. Save Energy ..!!

Friday, February 19, 2010

technocrat

Hey .. hi guys.

Technocrat, a perfect terminology to represent an expert who is a member of a highly skilled elite group. Just a thought came in mind to blog on technical topics and the first word came in mind is technocrat.

I am Rajiv Karambalkar, working as a software engineer with Persistent Systems. I Love maths, aspired to be an engineer, obviously not as software engineer.

This is my technical blog. I will be posting some technical stuffs..

Do do visit this blog.

Bye geeks, I will be back with some technical stuff..