tags:

views:

92

answers:

6
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.

+3  A: 

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!

Bogdan Constantinescu
The code is probably an event handler (probably onsubmit for the form) so the `return false` will abort the form submission.Also this code is rather IE specific and will probably not work on many standard browsers. You should use `document.getElementById("frmMain").elements["POL_NO"]` instead of `document.frmMain.POL_NO`
Guss
that code is fine for all browsers. You can refer to a form by *name* like that. There'll be a form on that page defined like this: `<form name="frmMain">`. Accessing the input elements like that is fine too.
nickf
@nickf It's not fine at all. Best to choose standard-compliant methods over properietary or non-standard ones (which form access off of document really is, coming from the early DOM0 days).
kangax
A: 

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;

 }
Canavar
A: 

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".

Fabien Ménager
+3  A: 

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.

  1. If it doesn't (it's an empty string),
  2. it displays an alert (information window) and
  3. empties ENDT_NO field's value and
  4. puts focus on the POL_NO field - so the user can immediately start selecting/typing a value in this field
  5. 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.

Robert Koritnik
+3  A: 

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
Carson Myers
+7  A: 

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.

andynormancx