Hi All,
Iam using RSF(reasonable servlet faces) i need to clear the fields in the form after user submits it using javascript onload function. can anyone please help me.

here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:rsf="http://ponder.org.uk/rsf">
<head>
<title rsf:id="basic-page-title">Sakai Skin for FlowTalk</title>
<link  rsf:id="scr=portal-matter" href="/library/skin/tool_base.css" type="text/css" rel="stylesheet" media="all"/>
<link href="/library/skin/rsmart/tool.css" type="text/css" rel="stylesheet" media="all" rsf:id="scr=portal-matter" />
<style type="text/css">
<!--

.prop {
    height:130px;
  }

}

-->
</style>

<script>
<!-- 
var minYear=1940;
var maxYear=2011;

function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
        if (i==2) {this[i] = 29}
   } 
   return this
}

function dateRange(dtStr){
    var daysInMonth = DaysArray(12)
    var strMonth=dtStr.substring(2,4)
    var strDay=dtStr.substring(0,2)
    var strYear=dtStr.substring(4,9)
    strYr=strYear
    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    }
    month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)
    if(isNaN(dtStr)){
        alert("The date format should be : ddmmyyyy")
        document.getElementById("dob").value = "";
       document.frm.dob.focus();
       return false;
    }


    if (dtStr.length != 8){
        alert("The date format should be : ddmmyyyy")
        document.getElementById("dob").value = "";
       document.frm.dob.focus();
       return false;
    }
    if (strMonth.length<1 || month<1 || month>12){
        alert("Please enter a valid month")
        document.getElementById("dob").value = "";
       document.frm.dob.focus();
       return false;
    }
    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        alert("Please enter a valid day")
        document.getElementById("dob").value = "";
       document.frm.dob.focus();
       return false;
    }
    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
        document.getElementById("dob").value = "";
       document.frm.dob.focus();
       return false;
    }

return true
}

function ValidateForm(){
    var dt=document.frmSample.txtDate
    if (isDate(dt.value)==false){
        dt.focus()
        return false
    }
    return true
 }
 -->
</script>
<script type="text/javascript" language="JavaScript" >
var busy=0;
function getValid(pf)
{
    if(pf=="")
    {
    alert("please enter a valid pfindexno");
    document.getElementById("pfindexno").value="";
    return false;
    }
    if(isNaN(pf))
    {
    alert("please enter a valid pfindexno");
    document.getElementById("pfindexno").value="";
    return false;
    }
}


function validateDate(strdate)
{
    if(8 > strdate.length) 
    {
    alert("Error:Enter Valid Date");
    }
    if(isNaN(strdate))
    {
    alert("Error:Enter Valid Date");
    }
    }

function emailValidator(varaemail){

    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

if(!varaemail.match(emailExp))
    {
        alert("Error: Invalid mail id");
        document.getElementById("input").value="";
        return false;
            }


}
function newWindow(newContent){
  winContent = window.open(newContent, 'nextWin', 'right=0, top=20,width=750,height=600, toolbar=no,scrollbars=yes, resizable=no') }

function showSdateCal()
{
   var string2 = "dob";
  //alert(string2);
  var string2val = document.getElementById(string2).value;
  var dt;
    if((null == string2val) || (string2val.length == 0)) dt = new Date();
  else dt = new Date(document.getElementById(string2).value);

   if (!isNaN(dt))
  { 
    var cal2 = new calendar2(document.getElementById(string2));
    cal2.popup();
    document.getElementById(string2).select();
  }

}
function clearOnly()
{
 document.getElementById("pfindexno").value="";
 document.getElementById("dob").value="";
 document.getElementById("input").value="";


}
</script>
</head>




<body rsf:id="scr=sakai-body" onload="clearOnly();">

<div class="portletBody">

<div class="toplinkSbi">
<a href="/xsl-portal" target="_parent" rsf:id="msg=mainText2">Home</a>
</div>
        <div class="sbiHeader">
    <div class="topSbi">
    &nbsp;
    </div>
    </div>

    <div class="navIntraTool" width="100%">
    <h3 rsf:id="msg=mainTitle">reset your password</h3></div>

    <div class="navIntraTool" width="100%">
    <span><strong>Fields with</strong><span style="color: red; font-weight: bold;">&nbsp;*&nbsp;</span><strong>are mandatory</strong></span>
    </div>


 <form rsf:id="form" name="forgot">

<div rsf:id="error-row:">
        <div class="alertMessage" rsf:id="error"></div>
</div>
<br/>
 <table align="center">
<tr>
<td style="color: red;">*</td>
  <td><label rsf:id="msg=formLabel" >PF </label></td>
<td><input rsf:id="pfindexno" id="pfindexno" type="text" onBlur = "getValid(this.value);"/></td>
<td>&nbsp;</td>
</tr>
<tr>
<td style="color: red;">*</td>
  <td><label  rsf:id="msg=formLabel3" >Date </label></td>
  <td><input rsf:id="dob" id="dob" type="text"  onBlur = "dateRange(this.value);"/></td>
  <td><font color="blue" rsf:id="msg=formLabel5">Format</font></td>
</tr>
 <tr>
<td style="color: red;">*</td>
  <td><label  rsf:id="msg=formLabel2">email</label></td>
  <td><input rsf:id="input" id="input" type="text"  onBlur = "emailValidator(this.value);" /></td>
  <td>&nbsp;</td>
</tr> 


<tr>
<td></td>
<td></td>
</tr>

<tr>
<td>&nbsp;</td>
  <td  align="center">&nbsp;</td>
  <td align="center"><input name="submit" type="submit" value="Submit" rsf:id="submit" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr><td> <div class="prop"> </div></td></tr>

<div align="center" id="footer">

<div class="footerExtNav" align="center"></div>
<div id="footerInfo">
<span class="skip"></span>
<br />
</div>
</div>

</table>
 </form>
 </div>

</body>
</html>

Recommended Answers

All 9 Replies

Hey,why do you put large amount of code for a little task?

create one function


<script language="javascript">
function clearAll()
{
document.getElementById('urInputFieldName').value="";
//Do this for all other input fields
}
</script>

And then call it in this way

<body onLoad="clearAll();">
.
...

i tried that code but its not working

But why is it not working??
Did you find any error or what?

It is not giving any error but the fields are not cleared previous data remains when i return to the page.

Ohh,so this is your real question,that when you leave a particular page and then return back,the page should not contain any values.

exactly , now can u suggest me with some solution

hey,so you need to clear the cache in your code somewhere.
Try to put this

A page that Internet Explorer is browsing is not cached until half of the 64 KB buffer is filled. Usually, metatags are inserted in the header section of an HTML document, which appears at the beginning of the document. When the HTML code is parsed, it is read from top to bottom. When the <HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"> metatag is read, Internet Explorer looks for the existence of the page in cache at that exact moment. If it is there, it is removed.

To properly prevent the Web page from appearing in the cache, place another header section at the end of the HTML document. For example:

<HTML>
<HEAD>
<META HTTP-EQUIV="REFRESH" CONTENT="5">
<TITLE> Pragma No-cache </TITLE>
</HEAD>
<BODY>
This is an example of where to place the second header section<br>
so that the "Pragama, No-Cache" metatag will work as it is supposed to.<br>
</BODY>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</HTML>

If you wish you can remove the second head section.

Try this. :)

i tried this but that doesn't work too

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.