How can I reset the value of an individual input field to its default? I have a function which validates the input value when it is entered (onChange), and if the entered value is invalid, I want to reset just this input value to its default. Other input values that has already been validated should not be reset.

Jayesh Nazre

Well there is no direct way, but you can write a JavaScript function in the onFocus event to save the old data and then in the onChange event you can reassign the old data if there is an error. Here is a sample code for that.

Enter any data in the text field other that "1". Then again go back to the text field and enter "1" then tab out of the text field. The original data will be reassigned to the text field.


<html>
<head>
<script language=javascript>
var temp_data;
function test() {
  if (document.frm_test.txt_1.value == "1") {
    alert ("1 should note be entered");
    document.frm_test.txt_1.value = temp_data;
    return -1;
  }
}

function savedata() {
  temp_data = document.frm_test.txt_1.value;
}
</script>
</head>
<body>

<form name=frm_test id=frm_test>

<input type=text name=txt_1 id=txt_1 
   onchange=test() onfocus=savedata()>


</form>

</body>
</html>
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact