Victor Charles
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.toStri ng().index Of("_");
var parentName = event.srcElement.id.toStri ng().subst ring(0, endIndex);
var mskinput = document.getElementById(pa rentName);
var txb = document.getElementById(ev ent.srcEle ment.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;
}
}
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
function onfocusout() {
if (activeControl != null) {
activeControl = null;
}
else {
var endIndex = event.srcElement.id.toStri
var parentName = event.srcElement.id.toStri
var mskinput = document.getElementById(pa
var txb = document.getElementById(ev
if (mskinput.control.get_text
alert("Invalid Input");
activeControl = txb;
StartTheTimer();
secs = 1;
inFocus = true;
}
else {
inFocus = false;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The runtime error indicates that:
var txb = document.getElementById('C 1RotationM in_C1TextB ox');
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.
var txb = document.getElementById('C
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.
ASKER
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_OnClientFocu s(sender, e) {
var control = sender.get_id() + "_C1TextBox";
checkFocus(control);
};
function C1RotationMin_OnClientFocu s(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(co ntrol);
var FieldRange = text.createTextRange();
FieldRange.moveStart('char acter', 0);
FieldRange.collapse();
FieldRange.select();
}
}
window.onload = function () {
var txb = document.getElementById('R otationMin ');
txb.onfocusout = onfocusout;
var txb2 = document.getElementById('R otationMax ');
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("StartTheT imer()", delay)
}
}
function onfocusout() {
if (activeControl != null) {
activeControl = null;
}
else {
var endIndex = event.srcElement.id.toStri ng().index Of("_");
var parentName = event.srcElement.id.toStri ng().subst ring(0, endIndex);
var mskinput = document.getElementById(pa rentName);
var txb = document.getElementById(ev ent.srcEle ment.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;
}
}
}
Code:
var inFocus = false;
function C1RotationMax_OnClientFocu
var control = sender.get_id() + "_C1TextBox";
checkFocus(control);
};
function C1RotationMin_OnClientFocu
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(co
var FieldRange = text.createTextRange();
FieldRange.moveStart('char
FieldRange.collapse();
FieldRange.select();
}
}
window.onload = function () {
var txb = document.getElementById('R
txb.onfocusout = onfocusout;
var txb2 = document.getElementById('R
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("StartTheT
}
}
function onfocusout() {
if (activeControl != null) {
activeControl = null;
}
else {
var endIndex = event.srcElement.id.toStri
var parentName = event.srcElement.id.toStri
var mskinput = document.getElementById(pa
var txb = document.getElementById(ev
if (mskinput.control.get_text
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
ASKER
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="C1RotationM in_OnClien tFocus"/>
</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="C1RotationM ax_OnClien tFocus"/>
</tr>
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="C1RotationM
</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="C1RotationM
</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('R otationMin ');
var txb = document.getElementById('C 1RotationM in_C1TextB ox');
You should probably have:
var txb = document.getElementById('C 1RotationM in');
You've had:
var txb = document.getElementById('R
var txb = document.getElementById('C
You should probably have:
var txb = document.getElementById('C
ASKER
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_OnClientFoc us(sender, e) {
var control = sender.get_id() + "_C1TextBox";
checkFocus(control);
};
function C1MaskedInput2_OnClientFoc us(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(co ntrol);
var FieldRange = text.createTextRange();
FieldRange.moveStart('char acter', 0);
FieldRange.collapse();
FieldRange.select();
}
}
window.onload = function () {
var txb = document.getElementById('C 1MaskedInp ut1_C1Text Box');
txb.onfocusout = onfocusout;
var txb2 = document.getElementById('C 1MaskedInp ut2_C1Text Box');
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("StartTheT imer()", delay)
}
}
function onfocusout() {
if (activeControl != null) {
activeControl = null;
}
else {
var endIndex = event.srcElement.id.toStri ng().index Of("_");
var parentName = event.srcElement.id.toStri ng().subst ring(0, endIndex);
var mskinput = document.getElementById(pa rentName);
var txb = document.getElementById(ev ent.srcEle ment.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="C1MaskedInp ut1_OnClie ntFocus" />
<cc1:C1MaskedInput ID="C1MaskedInput2" runat="server" Mask="999,99" Width="155px"
OnClientFocus="C1MaskedInp ut2_OnClie ntFocus" />
<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>
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_OnClientFoc
var control = sender.get_id() + "_C1TextBox";
checkFocus(control);
};
function C1MaskedInput2_OnClientFoc
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(co
var FieldRange = text.createTextRange();
FieldRange.moveStart('char
FieldRange.collapse();
FieldRange.select();
}
}
window.onload = function () {
var txb = document.getElementById('C
txb.onfocusout = onfocusout;
var txb2 = document.getElementById('C
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("StartTheT
}
}
function onfocusout() {
if (activeControl != null) {
activeControl = null;
}
else {
var endIndex = event.srcElement.id.toStri
var parentName = event.srcElement.id.toStri
var mskinput = document.getElementById(pa
var txb = document.getElementById(ev
// if (mskinput.control.get_text
if (mskinput.control.get_text
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="C1MaskedInp
<cc1:C1MaskedInput ID="C1MaskedInput2" runat="server" Mask="999,99" Width="155px"
OnClientFocus="C1MaskedInp
<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.
ASKER
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
Any ideas why it is still not working?
Code:
var inFocus = false;
function C1RotationMax_OnClientFocu
var control = sender.get_id() + "_C1TextBox";
checkFocus(control);
};
function C1RotationMin_OnClientFocu
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(co
var FieldRange = text.createTextRange();
FieldRange.moveStart('char
FieldRange.collapse();
FieldRange.select();
}
}
window.onload = function () {
var txb = document.getElementById('C
txb.onfocusout = onfocusout;
var txb2 = document.getElementById('C
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("StartTheT
}
}
function onfocusout() {
if (activeControl != null) {
activeControl = null;
}
else {
var endIndex = event.srcElement.id.toStri
var parentName = event.srcElement.id.toStri
var mskinput = document.getElementById(pa
var txb = document.getElementById(ev
if (mskinput.control.get_text
alert("Invalid Input");
activeControl = txb;
StartTheTimer();
secs = 1;
inFocus = true;
}
else {
inFocus = false;
}
}
}