Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with Javascript statement

Hi,

How do you include in "And" statement withing a javacript code?

I amd trying the code below but it does not work.

 if (mskinput.control.get_text().trimEnd().length > 0 and mskinput.control.get_text().trimEnd().length < 5) {


 function onfocusout() {

            if (activeControl != null) {
                activeControl = null;
            }
            else {
                var endIndex = event.srcElement.id.toString().indexOf("_");
                var parentName = event.srcElement.id.toString().substring(0, endIndex);
                var mskinput = document.getElementById(parentName);
                var txb = document.getElementById(event.srcElement.id);
                if (mskinput.control.get_text().trimEnd().length > 0 and mskinput.control.get_text().trimEnd().length < 5) {
                    alert("Invalid Input");
                    activeControl = txb;
                    StartTheTimer();
                    secs = 1;
                    inFocus = true;
                }
                else {
                    inFocus = false;
                }
            }
ASKER CERTIFIED SOLUTION
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

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
Avatar of Victor  Charles

ASKER

Hi,

Below is the complete code. When the project initially loads, I receive the following error:
Microsoft JScript runtime error: 'null' is null or not an object

on line: txb.onfocusout = onfocusout;


When I click continue to ignore the error, the cursor goes to the far left when I click on the control, but the validation still does not work.

Validation:  if (mskinput.control.get_text().trimEnd().length > 0 && mskinput.control.get_text().trimEnd().length < 5) {


Any ideas why it is still not working?

Code:

   var inFocus = false;
    function C1RotationMax_OnClientFocus(sender, e) {
        var control = sender.get_id() + "_C1TextBox";
        checkFocus(control);

    };

    function C1RotationMin_OnClientFocus(sender, e) {
        var control = sender.get_id() + "_C1TextBox";
        checkFocus(control);

    };

    function checkFocus(control) {
        if (inFocus == true && activeControl != null) {
            activeControl.focus();
            return false;
        }
        else {

            var text = document.getElementById(control);

            var FieldRange = text.createTextRange();
            FieldRange.moveStart('character', 0);
            FieldRange.collapse();
            FieldRange.select();
        }
    }

    window.onload = function () {
        var txb = document.getElementById('C1RotationMin_C1TextBox');
        txb.onfocusout = onfocusout;

        var txb2 = document.getElementById('C1RotationMax_C1TextBox');
        txb2.onfocusout = onfocusout;
    }


    var secs
    var activeControl = null;
    var timerID = null
    var timerRunning = false
    var delay = 100

    function StopTheClock() {
        if (timerRunning)
            clearTimeout(timerID)
        timerRunning = false
    }

    function StartTheTimer() {
        if (secs == 0) {
            StopTheClock()
            if (activeControl != null) {
                activeControl.focus();
                activeControl = null;
            }
        }
        else {
            self.status = secs
            secs = secs - 1
            timerRunning = true
            timerID = self.setTimeout("StartTheTimer()", delay)
        }
    }

    function onfocusout() {

        if (activeControl != null) {
            activeControl = null;
        }
        else {
            var endIndex = event.srcElement.id.toString().indexOf("_");
            var parentName = event.srcElement.id.toString().substring(0, endIndex);
            var mskinput = document.getElementById(parentName);
            var txb = document.getElementById(event.srcElement.id);
            if (mskinput.control.get_text().trimEnd().length > 0 && mskinput.control.get_text().trimEnd().length < 5) {
                alert("Invalid Input");
                activeControl = txb;
                StartTheTimer();
                secs = 1;
                inFocus = true;
            }
            else {
                inFocus = false;
            }
        }
    }
The runtime error indicates that:

var txb = document.getElementById('C1RotationMin_C1TextBox');

is returning Null. Make sure you have an element with an ID of C1RotationMin_C1TextBox on your page - double check spelling and case.

FYI - onfocusout doesn't work in Firefox - use onblur instead.
The ID of my controls are RotationMin and RotationMax. I tried the following code but still the same error.

Code:

   var inFocus = false;
    function C1RotationMax_OnClientFocus(sender, e) {
        var control = sender.get_id() + "_C1TextBox";
        checkFocus(control);

    };

    function C1RotationMin_OnClientFocus(sender, e) {
        var control = sender.get_id() + "_C1TextBox";
        checkFocus(control);

    };

    function checkFocus(control) {
        if (inFocus == true && activeControl != null) {
            activeControl.focus();
            return false;
        }
        else {

            var text = document.getElementById(control);

            var FieldRange = text.createTextRange();
            FieldRange.moveStart('character', 0);
            FieldRange.collapse();
            FieldRange.select();
        }
    }

    window.onload = function () {
        var txb = document.getElementById('RotationMin');
        txb.onfocusout = onfocusout;

        var txb2 = document.getElementById('RotationMax');
        txb2.onfocusout = onfocusout;
    }


    var secs
    var activeControl = null;
    var timerID = null
    var timerRunning = false
    var delay = 100

    function StopTheClock() {
        if (timerRunning)
            clearTimeout(timerID)
        timerRunning = false
    }

    function StartTheTimer() {
        if (secs == 0) {
            StopTheClock()
            if (activeControl != null) {
                activeControl.focus();
                activeControl = null;
            }
        }
        else {
            self.status = secs
            secs = secs - 1
            timerRunning = true
            timerID = self.setTimeout("StartTheTimer()", delay)
        }
    }

    function onfocusout() {

        if (activeControl != null) {
            activeControl = null;
        }
        else {
            var endIndex = event.srcElement.id.toString().indexOf("_");
            var parentName = event.srcElement.id.toString().substring(0, endIndex);
            var mskinput = document.getElementById(parentName);
            var txb = document.getElementById(event.srcElement.id);
            if (mskinput.control.get_text().trimEnd().length > 0 && mskinput.control.get_text().trimEnd().length < 5) {
                alert("Invalid Input");
                activeControl = txb;
                StartTheTimer();
                secs = 1;
                inFocus = true;
            }
            else {
                inFocus = false;
            }
        }
    }
Any chance you can post the full code - preferably as a live link. If not post the HTML for your form and the javascript
Hi,

Below is the aspx code:

     260-Rotation Minimum [rpm]</td>
                    <td class="style40" bgcolor="Silver" >
                        <cc2:C1MaskedInput ID="C1RotationMin" runat="server" Mask="999,999"
                            Width="50px" style="top: 0px; left: 0px" OnClientFocus="C1RotationMin_OnClientFocus"/>
                    </td>
                  </tr>
            <tr>
                 <td class="style1034" bgcolor="Silver" >
                     261-Rotation Maximum [rpm]</td>
                 <td class="style40" bgcolor="Silver" >
                     <cc2:C1MaskedInput ID="C1RotationMax" runat="server" Mask="999,999"
                         Width="50px" style="top: 0px; left: 0px" OnClientFocus="C1RotationMax_OnClientFocus"/>
                           </tr>
I don't know a lot about ASP but by the looks of your code your inputs have IDs of C1RotationMax and C1RotationMin which differs from both your previous posts.

You've had:
     var txb = document.getElementById('RotationMin');
     var txb = document.getElementById('C1RotationMin_C1TextBox');

You should probably have:
     var txb = document.getElementById('C1RotationMin');
Hi

Below is the sample that works, the only chage made was changing the C1MaskedInput1 and C1MaskedInput2 to RotationMin and RotationMax. I think C1TextBox has to be part of the code.


 <script type="text/javascript">

        var inFocus = false;
        function C1MaskedInput1_OnClientFocus(sender, e) {
            var control = sender.get_id() + "_C1TextBox";
            checkFocus(control);

        };

        function C1MaskedInput2_OnClientFocus(sender, e) {
            var control = sender.get_id() + "_C1TextBox";
            checkFocus(control);

        };

        function checkFocus(control) {
            if (inFocus == true && activeControl != null) {
                activeControl.focus();
                return false;
            }
            else {
             
                var text = document.getElementById(control);

                var FieldRange = text.createTextRange();
                FieldRange.moveStart('character', 0);
                FieldRange.collapse();
                FieldRange.select();
            }
        }

        window.onload = function () {
            var txb = document.getElementById('C1MaskedInput1_C1TextBox');
            txb.onfocusout = onfocusout;

            var txb2 = document.getElementById('C1MaskedInput2_C1TextBox');
            txb2.onfocusout = onfocusout;
        }


        var secs
        var activeControl = null;
        var timerID = null
        var timerRunning = false
        var delay = 100

        function StopTheClock() {
            if (timerRunning)
                clearTimeout(timerID)
            timerRunning = false
        }

        function StartTheTimer() {
            if (secs == 0) {
                StopTheClock()
                if (activeControl != null) {
                    activeControl.focus();
                    activeControl = null;
                }
            }
            else {
                self.status = secs
                secs = secs - 1
                timerRunning = true
                timerID = self.setTimeout("StartTheTimer()", delay)
            }
        }

        function onfocusout() {

            if (activeControl != null) {
                activeControl = null;
            }
            else {
                var endIndex = event.srcElement.id.toString().indexOf("_");
                var parentName = event.srcElement.id.toString().substring(0, endIndex);
                var mskinput = document.getElementById(parentName);
                var txb = document.getElementById(event.srcElement.id);
              //  if (mskinput.control.get_text().trimEnd().length > 0 and mskinput.control.get_text().trimEnd().length < 5)
                if (mskinput.control.get_text().trimEnd().length > 0 && mskinput.control.get_text().trimEnd().length < 5) {
                    alert("Invalid Input");
                    activeControl = txb;
                    StartTheTimer();
                    secs = 1;
                    inFocus = true;
                }
                else {
                    inFocus = false;
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
   

    <cc1:C1MaskedInput ID="C1MaskedInput1" runat="server" Mask="999,99" Width="155px"
        OnClientFocus="C1MaskedInput1_OnClientFocus" />
    <cc1:C1MaskedInput ID="C1MaskedInput2" runat="server" Mask="999,99" Width="155px"
        OnClientFocus="C1MaskedInput2_OnClientFocus" />
    <asp:Button ID="Button1" runat="server" Text="Retrieve Text" />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <br />
    Text is
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>
As I said, I don't know ASP - have a look at the source of your page in a browser and see what the IDs are set to. That's what you need to pass into your txb assignments.