Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke an event after losing focus from a textbox that was just filled out?

I have two text boxes: A and B using Html.textboxfor. TextBox 'A' is enabled and TextBox 'B' is disabled.

When I type values in Textbox 'A' and change focus away from the textbox, TextBox 'B' should be populated with some value.

For example: I am entering the value of TextBox A = "King"; if the focus is lost from the box, the value in the Textbox B should be "Peter", which should be filled automatically.

like image 937
SRA Avatar asked Oct 15 '22 03:10

SRA


2 Answers

you can jsut give the textbox an attrbiute of onblur and call a method which will fill the second textbox

something like:

   <script language="javascript">
       function fllB()
       {
          var txtB = document.getElementById("txtB");
          var txtA = document.getElementById("txtA");

          if (txtA.value == "King")
          {
              txtB.value = "Peter";
          }
        }
    </script>

<input type="text" id="txtA" onblur="fillB()" />
<input type="text" id="txtB" />
like image 128
guy schaller Avatar answered Nov 01 '22 12:11

guy schaller


Below is the javascript function that I wrote to validate my input "Price" currency value.

<script language="javascript">
function validatePrice(textBoxId) {
    var textVal = textBoxId.value;
    var regex = /^(\$|)([1-9]\d{0,2}(\,\d{3})*|([1-9]\d*))(\.\d{2})?$/;
    var passed = textVal.match(regex);
    if (passed == null) {
        alert("Enter price only. For example: 523.36 or $523.36");
        textBoxId.Value = "";
    }
}

If you want to fire the validation after you lost a focus from text box, then use “onblur” event which is as same as OnLostFocus() in most of the WinForm controls.

<asp:TextBox ID="TextBox19" runat="server" Visible="False" Width="183px" 
                onblur="javascript:return validatePrice(this);"></asp:TextBox>
like image 22
S.Mishra Avatar answered Nov 01 '22 10:11

S.Mishra