admin管理员组文章数量:1024603
i have a form with two modes one that allows edit (value of text box)
and another mode for (non-manager) user - code behind adding "READONLY" to the TextBox tag.
so with the readonly 'mode' there's no input accepted in text box at all
my question is how could i execute validation method based on "READONLY" property of text box ?
strRo = "READONLY" on a condition
<asp:TextBox ID="txtNumbers" runat="server" <%=strRo %> onkeypress="return allowonlynumbers();" />
javascript function
function allowonlynumbers() {
if (event.keyCode >= 48 && event.keyCode <= 57) {
return true;
}
else {
alert('Only numbers can be entered');
return false;
}
}
i have a form with two modes one that allows edit (value of text box)
and another mode for (non-manager) user - code behind adding "READONLY" to the TextBox tag.
so with the readonly 'mode' there's no input accepted in text box at all
my question is how could i execute validation method based on "READONLY" property of text box ?
strRo = "READONLY" on a condition
<asp:TextBox ID="txtNumbers" runat="server" <%=strRo %> onkeypress="return allowonlynumbers();" />
javascript function
function allowonlynumbers() {
if (event.keyCode >= 48 && event.keyCode <= 57) {
return true;
}
else {
alert('Only numbers can be entered');
return false;
}
}
Share
Improve this question
asked Sep 24, 2012 at 16:06
LoneXcoderLoneXcoder
2,1637 gold badges40 silver badges77 bronze badges
2 Answers
Reset to default 3You would need to pass a reference to the element in your function call, so change the onkeypress
to this:
onkeypress="return allowonlynumbers(this);"
Then, your function needs to be like this:
function allowonlynumbers(obj) {
}
And the logic you can use is:
if (obj.readOnly) {
// The element is readonly
}
You should bind your event handler in javascript (why?). Then you will be able to use the this
operator inside of the handler to refer to the textbox, which you can check the readOnly
property of:
document.getElementById('test').onkeypress = function () {
if (!this.readOnly) {
if (event.keyCode >= 48 && event.keyCode <= 57) {
return true;
} else {
alert('Only numbers can be entered');
return false;
}
}
};
http://jsfiddle/FedsQ/
i have a form with two modes one that allows edit (value of text box)
and another mode for (non-manager) user - code behind adding "READONLY" to the TextBox tag.
so with the readonly 'mode' there's no input accepted in text box at all
my question is how could i execute validation method based on "READONLY" property of text box ?
strRo = "READONLY" on a condition
<asp:TextBox ID="txtNumbers" runat="server" <%=strRo %> onkeypress="return allowonlynumbers();" />
javascript function
function allowonlynumbers() {
if (event.keyCode >= 48 && event.keyCode <= 57) {
return true;
}
else {
alert('Only numbers can be entered');
return false;
}
}
i have a form with two modes one that allows edit (value of text box)
and another mode for (non-manager) user - code behind adding "READONLY" to the TextBox tag.
so with the readonly 'mode' there's no input accepted in text box at all
my question is how could i execute validation method based on "READONLY" property of text box ?
strRo = "READONLY" on a condition
<asp:TextBox ID="txtNumbers" runat="server" <%=strRo %> onkeypress="return allowonlynumbers();" />
javascript function
function allowonlynumbers() {
if (event.keyCode >= 48 && event.keyCode <= 57) {
return true;
}
else {
alert('Only numbers can be entered');
return false;
}
}
Share
Improve this question
asked Sep 24, 2012 at 16:06
LoneXcoderLoneXcoder
2,1637 gold badges40 silver badges77 bronze badges
2 Answers
Reset to default 3You would need to pass a reference to the element in your function call, so change the onkeypress
to this:
onkeypress="return allowonlynumbers(this);"
Then, your function needs to be like this:
function allowonlynumbers(obj) {
}
And the logic you can use is:
if (obj.readOnly) {
// The element is readonly
}
You should bind your event handler in javascript (why?). Then you will be able to use the this
operator inside of the handler to refer to the textbox, which you can check the readOnly
property of:
document.getElementById('test').onkeypress = function () {
if (!this.readOnly) {
if (event.keyCode >= 48 && event.keyCode <= 57) {
return true;
} else {
alert('Only numbers can be entered');
return false;
}
}
};
http://jsfiddle/FedsQ/
本文标签: javascriptvalidating textbox checking if it isn39t readonly firstStack Overflow
版权声明:本文标题:javascript - validating textbox checking if it isn't readonly first - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609267a2158907.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论