Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Sunday, August 15, 2010

JavaScript To Copy Text From One Field To Another

Often when creating a form on a web page, you need your customers to fill out a field such as a mailing address, as well as a billing address. Instead of having your customers fill out the form twice, you can use JavaScript to copy the form's data from one field to another.
Mailing Address 

Name:  
City:  
 Check this box if Billing Address and Mailing Address are the same.Billing Address 

Name:  
City: 


The JavaScript used to achieve the effect looks like this:


function FillBilling(f) {
  if(f.billingtoo.checked == true) {
    f.billingname.value = f.shippingname.value;
    f.billingcity.value = f.shippingcity.value;
  }
}
To add more fields, just add to the parameters shown above...like this:
f.billingstate.value = f.shippingstate.value;
    f.billingzip.value = f.shippingzip.value;
 The HTML for the form you will use looks like this:


 <b>Mailing Address</b>
<br><br>
<form>
<input type="text" name="shippingcity">
<br>
<input type="checkbox" name="billingtoo" onclick="FillBilling(this.form)">
<em>Check this box if Billing Address and Mailing Address are the same.</em>
<P>
<b>Billing Address</b>
<br><br>
Name:
<input type="text" name="billingname">
<br>
City:
<input type="text" name="billingcity">
</form>












Thursday, August 12, 2010

Javascript function for email validation

 Email Validator In JSF Using JavaScript
In Javascript write the following function

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){
        alert("Invalid E-mail ID")
        return false
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        alert("Invalid E-mail ID")
        return false
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(at,(lat+1))!=-1){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(dot,(lat+2))==-1){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(" ")!=-1){
        alert("Invalid E-mail ID")
        return false
    }

    return true
}

function validateUser()
{
    var flag = true;   
    var mailid = document.frmuser.tf_USER_MAILID;//frmuser is the form id

    if(mailid != null && mailid.value == "" ){
        flag = false;
        alert ("Mailid cannot be Empty...");
    }

    if (flag ) {
            flag =  emailcheck(mailid.value);
        }

    return flag;
}

In JSP:

Include javascript file into jsp using following code  
Call the javascript function using following code
<h:commandButton id="bt_SAVE" value="SAVE" onclick="javascript:return validateUser();" action="#{createuser.saveUserDetails}" style="font-size:16;font-weight:bold"/>

JSF Related topics: JCaptcha in JSF, Integrating Richfaces with JSF,Getting client and server sessionId in JSF,PopUp AlertMessagebox for JSF and more.....

Friday, June 18, 2010

Javascript function to Refresh the page every one minute

In jsp include the javascript file in the following way

<script type='text/javascript' src="<%=request.getContextPath()%>/user/javascript/refreshdetails.js"></script>
<script type="text/javascript">
window.setInterval("refresh()",60000);
</script>

In refreshdetails.js file write the following function

function refresh(){
location.reload(true);
}

This will refreshes the page for every 1 minute

Thursday, June 10, 2010

Address Validator using Java Script

//for Address
function Address(evt) {

evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
//alert(charCode)
if((charCode > 32 & charCode < 38)|(charCode > 38 & charCode < 44)|charCode==31 |(charCode > 57 & charCode < 65) |(charCode > 90 & charCode < 97)|(charCode > 122 & charCode < 127) ){

return false
}
status = ""
return true
}

Wednesday, May 19, 2010

JavaScript function to restrict SpecialCharacter

1) Creating javascript file(util.js) and create function NoSpecialCharacter()

//for NoSpecialCharacter
function NoSpecialCharacter(evt) {

evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
//alert(charCode)
if((charCode > 30 & charCode < 48)|charCode==32 |(charCode > 57 & charCode < 65) |(charCode > 90 & charCode < 97)|(charCode > 122 & charCode < 127) ){
status = "This field accepts No Special Character."
return false
}
status = ""
return true
}

2) Include util.js file in jsp file

<script type='text/javascript' src="<%=request.getContextPath()%>/utils.js"></script>


3)Calling javascript function in jsp
<h:inputText id="tf_NAME" label="Name" autocomplete="off" onkeypress="return NoSpecialCharacter(event);" >

JavaScript function to restrict input to only characters and space

1) Creating javascript file(util.js) and create function onlyCharSpace()

//only characters and space
function onlyCharSpace(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
//alert(charCode)
if((charCode > 31 & charCode < 49)|(charCode > 49 & charCode < 65)|(charCode > 90 & charCode < 97) |(charCode > 122 & charCode < 127)){
status = "This field accepts characters and space only."
return false
}
status = ""
return true
}

2) Include util.js file in jsp file

<script type='text/javascript' src="<%=request.getContextPath()%>/utils.js"></script>


3)Calling javascript function in jsp
<h:inputText id="tf_NAME" label="Name" autocomplete="off" onkeypress="return onlyCharSpace(event)" >

Tuesday, May 18, 2010

JavaScript function to restrict input to only characters

1) Creating javascript file(util.js) and create function onlyCharNoSpace()

//only characters
function onlyCharNoSpace(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
//alert(charCode)
if((charCode > 31 & charCode < 65) |(charCode > 90 & charCode < 97) |(charCode > 122 & charCode < 127)){
status = "This field accepts characters only."
return false
}
status = ""
return true
}

2) Include util.js file in jsp file

<script type='text/javascript' src="<%=request.getContextPath()%>/utils.js"></script>


3)Calling javascript function in jsp
<h:inputText id="tf_NAME" label="Name" autocomplete="off" onkeypress="return onlyCharNoSpace(event)" >

JavaScript function to restrict input to numeric

1) Creating javascript file(util.js) and create function checkIt()
//only numbers
function onlyNum(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
status = "This field accepts numbers only."
return false
}
status = ""
return true
}

2) Include util.js file in jsp file

<script type='text/javascript' src="<%=request.getContextPath()%>/utils.js"></script>

3)Calling javascript function in jsp
<h:inputText id="tf_NAME" label="Name" autocomplete="off" onkeyup="return onlyNum(event);" >

JavaScript function to make text caps

1) Creating javascript file(util.js) and create function makeCaps()

/**
This function get the data on every key press and it will change that data into caps
*/
function makeCaps(data){
var val = data.value;
data.value = val.toUpperCase();

}

2) Include util.js file in jsp file

<script type='text/javascript' src="<%=request.getContextPath()%>/utils.js"></script>

3)Calling javascript function in jsp
<h:inputText id="tf_NAME" label="Name" autocomplete="off" onkeyup="makeCaps(this)">

JavaScript to disable F5 button

<script language="javascript" >
function checkKeyCode(evt)
{

var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if(event.keyCode==116)
{
alert("F5 has been Disabled");
evt.keyCode=0;
return false
}
}
document.onkeydown=checkKeyCode;
</script >

Java Script to Disabling Right Click in Web Browser

<script language="javascript" >
//Disable right mouse click Script
var message="Right Click is Disabled!";

///////////////////////////////////
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(message);return false")

</script>

Java Script to Disabling Back Button in WebBrowser

<script language="javascript" >
function noBack(){window.history.forward()}
noBack();
window.onload=noBack;
window.onpageshow=function(evt){if(evt.persisted)noBack()}
window.onunload=function(){void(0)}
</script>