Javascript Example-function to validate textbox on blank or default text


If you have  textbox in a form,when a user submits the form,it should check whether the user has filled it or not .The below javascript code will check whether a use filled the text box or left it the default value.this query will validate textbox. 

I am trying to validate a text box so that if the default value for the text box or a blank value is provided, it prevents the form from being submitted.


Javascript code is as follows


  <script  >
   function checkForm()
    {
  
    if (form1.elements['txttext'].value == "" || form1.elements['txttext'].value == "Default Value")
    {
        alert("Please Enter the value in textbox");
        form1.elements['txttext'].focus();
        return false;
    }
      else
       {
        form1.submit();
        return true;
       }
       return true;
}

</script>

 Aspx page code is as follows

<asp:TextBox ID="txttext" runat="server" Text="Default Value"></asp:TextBox>
    <asp:Button ID="btnsubmit" runat="server" Text="submit" OnClientClick="return checkForm()" />

Tags:

Join Us!