本文介绍了Onblur在被召唤之前被解雇。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
--scenario
<pre>A textbox is there. I am allowing user to enter only single dot.
Like 34.56
if 34. (then it will become 34)[which i am taking care of it in onblur]
--Now suppose the user enters a number like 342.53.
then message will appear "alert("Please enter numeric value!");"
--then i was making the text of textbox to be empty
--But suppose user enterd a big number like 34245.454.
then message will appear "alert("Please enter numeric value!");"
--then the textbox will be empty
--and the user has to retype again the number.
So text which i was making empty i removed that line.
So now user click a number 3465.45.
then message will appear "alert("Please enter numeric value!");"
So now user click a number 3465.45..
then message will appear "alert("Please enter numeric value!");"
But when i click outside the textbox then the number now is 3465.45..
---- --------------------------------
------------------------------------
So what i thought like in onblur function
i will check if it is a valid number or not. If it is not a valid number then I will make the text to be empty
----------- ---------------------------------
--------------------------------------------
<script type="text/javascript">
function validate1() {
var num = document.getElementById("txtid").value;
if (isNaN(num) == true) {
alert("Please enter numeric value!");
return false;
}
else
return true;
}
</script>
<script type="text/javascript">
function onblurvalidate() {
var num = document.getElementById("txtid").value;
if (isNaN(num) == true) {
document.getElementById("txtid").value = "";
return false;
}
var length = num.length;
var num1 = num + '';
var dotindex = num1.indexOf(".");
if (length == (dotindex+1))
{
document.getElementById("txtid").value = num.substring(0, num.length - 1);
}
}
</script>
----------- ------------------
-----------------------------
But to my amazement i got the result like when i entered a wrong number suppose
34.45.
the message appeared "alert("Please enter numeric value!");"
And the text is empty.
How this is possible. As I have not clicked outside the textbox,So it should not fire. Kindly help me understanding this.
--------------------------- ---
< asp:TextBox ID =txtidCssClass =textBox_for6tdsrunat =serveronkeyup =javascript:validate1(); onblur =onblurvalidate()>
------------------------------
<asp:TextBox ID="txtid" CssClass="textBox_for6tds" runat="server" onkeyup="javascript:validate1();" onblur="onblurvalidate()">
推荐答案
这篇关于Onblur在被召唤之前被解雇。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!