Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 541
  • Last Modified:

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;
                }
            }
0
vcharles
Asked:
vcharles
  • 4
  • 4
1 Solution
 
lwadwellCommented:
use && i.e.
if (mskinput.control.get_text().trimEnd().length > 0 && mskinput.control.get_text().trimEnd().length < 5) {
0
 
vcharlesAuthor Commented:
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;
            }
        }
    }
0
 
Chris StanyonCommented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vcharlesAuthor Commented:
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;
            }
        }
    }
0
 
Chris StanyonCommented:
Any chance you can post the full code - preferably as a live link. If not post the HTML for your form and the javascript
0
 
vcharlesAuthor Commented:
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>
0
 
Chris StanyonCommented:
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');
0
 
vcharlesAuthor Commented:
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>
0
 
Chris StanyonCommented:
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now