if (document.frmMain.POL_NO.value == "")
{
alert("Select Policy Number");
document.frmMain.ENDT_NO.value="";
document.frmMain.POL_NO.focus();
return false;
}
Can anyone explain the above code to me? I am new to Javascript.
if (document.frmMain.POL_NO.value == "")
{
alert("Select Policy Number");
document.frmMain.ENDT_NO.value="";
document.frmMain.POL_NO.focus();
return false;
}
Can anyone explain the above code to me? I am new to Javascript.
Share Improve this question edited Oct 8, 2024 at 17:39 dumbass 27.3k4 gold badges37 silver badges74 bronze badges asked Sep 25, 2009 at 6:35 elsonelson 533 silver badges9 bronze badges 3- 3 Please choose a more meaningful title next time. – Georg Schölly Commented Sep 25, 2009 at 6:45
- 1 Don't change any code until you get literate with Javascript + DOM Manipulation + HTML – Robert Koritnik Commented Sep 25, 2009 at 6:50
- The code snippet is very familiar to me – Sathyajith Bhat Commented Sep 25, 2009 at 6:56
6 Answers
Reset to default 7It appears to be a bit of validation code to make sure a user has entered a value for an item referred to as "Policy Number". It is the sort of code that gets called when submitting a form to check that the values the user has entered are valid.
In detail:
if(document.frmMain.POL_NO.value == "")
Only run this code if the item called in POL_NO the form called frmMain doesn't have a value yet.
alert("Select Policy Number");
Display a message to tell the user that they need to enter a value.
document.frmMain.ENDT_NO.value="";
Set the ENDT_NO item of frmMain to a blank value.
document.frmMain.POL_NO.focus();
Set the focus to the POL_NO item (the same as the user tabbing to it or clicking on it).
return false;
Return false to the code that called the function that this code is in. If this code is used in the event handler for the submit button on a form then returning false will stop the form from being submitted to the server until the POL_NO item has a value.
If the contents of the item POL_NO
from the form frmMain
is empty, then throw an alert and change the value of the ENDT_NO
item value to nothing (empty) and after that focus on the element POL_NO
. Return false
after that (I assume this code executes at onSubmit
event, so the form won't be submitted if POL_NO
doesn't have a value)
Probably the logic is that the ENDT_NO
can't have a value if POL_NO
is empty.
Enjoy!
document.frmMain
is a form in the page, and POL_NO
and ENDT_NO
are fields in the form, presumably listboxes.
This code is a simple validation script to make sure you filled out the form correctly.
//if POL_NO hasn't been set (no policy number selected):
if(document.frmMain.POL_NO.value == "")
{
//show a message box
alert("Select Policy Number");
//clear the value (if any) of ENDT_NO
document.frmMain.ENDT_NO.value="";
//set the form focus to POL_NO (select it, as if you had clicked on it)
document.frmMain.POL_NO.focus();
//stop the form from being submitted
return false;
}
I'm assuming this code is part of a function which is called by frmMain
's onSubmit
event (and event handler) -- when the function returns false
the submit is cancelled. Were this not here, it would show the message box, clear ENDT_NO
, select POL_NO
and then submit anyways.
Note that referencing members of a form in the document.formName.fieldName.property
fashion is deprecated. The correct way is to use getElementById
or a similar function:
document.frmMain.ENDT_NO.value = ""; //bad
document.getElementById("ENDT_NO").value = ""; //correct
HTML document
your HTML document has this defined somewhere in its content
<form id="frmMain" ...>
<input type="..." id="POL_NO">
<input type="..." id="ENDT_NO">
</form>
SCRIPT code
So. Your script checks whether your POL_NO
input field has a value.
- If it doesn't (it's an empty string),
- it displays an alert (information window) and
- empties
ENDT_NO
field's value and - puts focus on the
POL_NO
field - so the user can immediately start selecting/typing a value in this field - returns false - probably to cancel form submission
Business logic
Based on the logic of this script, the business process obviously doesn't allow any value in ENDT_NO
field, until there's a value in POL_NO
.
Observation
If you need to change something about this code (if there's a bug in it), I strongly suggest you get to know Javascript/DOM/HTML before doing any changes.
Actually your code does a pretty simple validation, just read the code and find the fields POL_NO and ENDT_NO in your HTML output. Here is my ments :
// if your POL_NO field is empty,
if(document.frmMain.POL_NO.value == "")
{
// warn user that he need to select Policy number
alert("Select Policy Number");
// set ENDT_NO field's value to empty
document.frmMain.ENDT_NO.value="";
// set POL_NO active - focussed
document.frmMain.POL_NO.focus();
return false;
}
If the value of the input named POL_NO in the form frmMain is empty, then show a message "Select Policy Number", empty the input named ENDT_NO, give the focus to the input named POL_NO, and the exit the function with the return value "false".