Reputation: 53
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.
Upvotes: -1
Views: 763
Reputation: 13762
It 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.
Upvotes: 7
Reputation: 105029
your HTML document has this defined somewhere in its content
<form id="frmMain" ...>
<input type="..." id="POL_NO">
<input type="..." id="ENDT_NO">
</form>
So. Your script checks whether your POL_NO
input field has a value.
ENDT_NO
field's value andPOL_NO
field - so the user can immediately start selecting/typing a value in this fieldBased 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
.
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.
Upvotes: 3
Reputation: 38564
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
Upvotes: 3
Reputation: 140205
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".
Upvotes: 0
Reputation: 48088
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 comments :
// 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;
}
Upvotes: 0
Reputation: 5356
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!
Upvotes: 3