locked
Script for Alert RRS feed

  • Question

  • User1769015664 posted

    The following code works but looks weird.

    On top of the Alert box, it says "localhost:55664 says." and alert is displayed on top of the page instead of the middle of the screen.

    Also, I want to add some colors to it.

        <script type="text/jscript">
            function NumericOnly(evt) {
                var iKeyCode = (evt.which) ? evt.which : evt.keyCode
                if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
                {
                    alert("Numbers only.");
                    return false;
                }
    
                return true;
            }
        </script>
    

    Monday, October 19, 2020 4:22 PM

Answers

  • User1769015664 posted

    Page_Load:

    txtID.Attributes.Add("onkeypress", "return NumericOnlyAllowed(event)");
        <script type="text/jscript">
            function NumericOnly(evt) {
                var iKeyCode = (evt.which) ? evt.which : evt.keyCode
                if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
                {
                    alert("Only Numbers are allowed");
                    return false;
                }
                return true;
            }
        </script>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 31, 2020 3:31 AM

All replies

  • User409696431 posted

    What a JavaScript alert looks like, and where it is positioned (if it is at the top, I'm guessing you are using Chrome) is controlled by the browser.  Many browsers indicate the page that called the alert.

    You don't have any control over this.

    If you want more control, use one of the many JavaScript or JQuery options for alerting, or use a different method for alerting the user.

    For example, if this is for data entered into a TextBox on a Web Form, use a validator to check that the entry is all numeric.

    <asp:RegularExpressionValidator id="RegularExpressionValidator1"
                       ControlToValidate="TextBox1"
                       ValidationExpression="\d+"
                       Display="Static"
                       EnableClientScript="true"
                       ErrorMessage="Please enter numbers only"
                       runat="server"/>

    Monday, October 19, 2020 8:01 PM
  • User1769015664 posted

    Thanks, but, I do not want the validator.

    Monday, October 19, 2020 8:12 PM
  • User409696431 posted

    My answer remains the same.  You don't control "alert".  Find another way.  Just google for "error message without using alert".  It's up to you to sort through them and find one that does what you want.

    Here are two examples: https://www.geeksforgeeks.org/how-to-display-error-without-alert-box-using-javascript/

    You can find others.

    Monday, October 19, 2020 8:50 PM
  • User1769015664 posted

    Page_Load:

    txtID.Attributes.Add("onkeypress", "return NumericOnlyAllowed(event)");
        <script type="text/jscript">
            function NumericOnly(evt) {
                var iKeyCode = (evt.which) ? evt.which : evt.keyCode
                if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
                {
                    alert("Only Numbers are allowed");
                    return false;
                }
                return true;
            }
        </script>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 31, 2020 3:31 AM