Saturday, February 16, 2013

Java Script Basics


Java Script validation:

String length validation:

This Tag Validate the String length by property of (“str.length”)
JavaScript validation is not secure as anybody can change what your script does in the browser. Using it for enhancing the visual experience is ok though.
Code :
var textBox = document.getElementById("myTextBox");
var textLength = textBox.value.length;
if(textLength > 5)
{
    //red
    textBox.style.backgroundColor = "#FF0000";
}
else
{
    //green
    textBox.style.backgroundColor = "#00FF00";
    // alert(“Something”);
}

Here the Code for Change the Color of text box , here We can add the Alert box too..

E-Mail Validation :

The function below checks if the content has the general syntax of an email.

This means that the input data must contain an @ sign and at least one dot (.). Also, the @ must not be the first character of the email address, and the last dot must be present after the @ sign, and minimum 2 characters before the end:

Code:

function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}

Tuesday, February 12, 2013

Servlet Practice 1 : retrieving all request, response, header elements


Request Handling Practices :

Retrieve the request parameters:

Usage : This Will allow to Retrieve All the Associated Request Parameters:

String values[];String temp1="";

Enumeration Enum=request.getParameterNames();
while(Enum.hasMoreElements()){
temp1=(String) Enum.nextElement();
values=request.getParameterValues(temp1);
if(values!=null){
for(String str:values)
System.out.println("Name : "+temp1+" && Values : "+str);
}
}

                                          ***********************
Retrieve the Header values:

Usage : This Will allow to Retrieve All the Associated Header Parameters:

String temp1="",temp2="";


Enumeration header=request.getHeaderNames();
while(header.hasMoreElements()){
temp1=(String) header.nextElement();
temp2=request.getHeader(temp1);
if(temp2!=null){
System.out.println(temp1+" && : "+temp2);
   }
}

                                         *************************


Setting Response Contents type:

Usage : This Will allow to set Response

PrintWriter out=response.getWriter();

response.setContentType("text/html");
out.println("<h1>Welcome "+firstname+"</h1><br><br>");
out.println("<h2>Your input Name : "+firstname+" "+lastname+"</h2>");
out.println("<h2>Ur Under "+category+" Category</h2>");

                                         **************************