Link to home
Start Free TrialLog in
Avatar of Mark Lee
Mark Lee

asked on

javascript form alert

<form>
<ntb:Combo id="myCombo" Mode="classic" >
<ntb:ComboTextBox
                     Width="300px" value="Keyword Search - Then Press Down Arrow"
                     DataFieldIndex=1>
</ntb:ComboTextBox>
....................etc etc
</ntb:Combo>
<input  type="submit"   value="Add Item" >
<form>

the documentation states that the Combo Object can be accessed by the following

myComboObject= document.getElementById("myCombo").object;

what I need is a javascript function that will give an alert
"Please Select An Item"
if the form is submitted and the value "Keyword Search - Then Press Down Arrow" is in the ComboTextBox field

in other words to stop some confusion when a new user simply clicks on the submit button before realising that they have not selected a search item first
Avatar of digitalseraphim
digitalseraphim

in your <head></head>

<script>
  function checkSubmit(){
      var myComboObject= document.getElementById("myCombo").object;
       if(myComboObject.GetTextValue() == "Keyword Search - Then Press Down Arrow"){
               alert("Please Select An Item");
               return false;
       }
        return true;
   }
</script>

then at your form:

<form ...  onsubmit="checkSubmit();">
<ntb:Combo id="myCombo" Mode="classic" >
<ntb:ComboTextBox
                     Width="300px" value="Keyword Search - Then Press Down Arrow"
                     DataFieldIndex=1>
</ntb:ComboTextBox>
....................etc etc
</ntb:Combo>
<input  type="submit"   value="Add Item" >
</form>
Avatar of Michel Plungjan
Yeah, I guess it is jsObject.  I was going by your earlier comment using ".object".  not sure what the difference is.
Avatar of Mark Lee

ASKER

the script works to produce the alert but then appears to want to submit the form still, the work around uses the   document.location =  history.go(0);
is there another way to get the script to just close the alert box, the return false does not appear to work in this case

<script>
  function checkSubmit(){
      var myComboObject= document.getElementById("myCombo").object;
       if(myComboObject.GetTextValue() == "Keyword Search - Then Press Down Arrow"){
               alert("Please Select An Item");
              document.location =  history.go(0);
               return false;
       }
        return true;
   }
</script>

try changing the onsubmit to the following:

onsubmit="return checkSubmit()"
This is not valid JS
    document.location =  history.go(0);
ASKER CERTIFIED SOLUTION
Avatar of digitalseraphim
digitalseraphim

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial