在我的项目中,我在注册期间检查用户的电子邮件 ID 是否有效,我正在使用 Javascript。我在单击按钮期间获取函数中文本框的值并验证它,但在单击它显示的按钮 Microsoft JScript 运行时错误:需要对象 错误 谁能指出我的代码中有什么问题,
我的 javascript 函数是,
function mailcheck() {
var txt = document.getElementById("emailid").value;
if (txt != "") {
var at = "@"
var dot = "."
var lat = str.indexOf(at)
var lstr = str.length
var ldot = str.indexOf(dot)
if (txt.indexOf(at) == -1) {
alert("Invalid E-mail ID")
obj.focus()
return false
}
if (txt.indexOf(at) == -1 || txt.indexOf(at) == 0 || txt.indexOf(at) == ltxt) {
alert("Invalid E-mail ID")
obj.focus()
return false
}
if (txt.indexOf(dot) == -1 || txt.indexOf(dot) == 0 || txt.indexOf(dot) == lstr) {
alert("Invalid E-mail ID")
obj.focus()
return false
}
if (str.indexOf(at, (lat + 1)) != -1) {
alert("Invalid E-mail ID")
obj.focus()
return false
}
if (txt.substring(lat - 1, lat) == dot || txt.substring(lat + 1, lat + 2) == dot) {
alert("Invalid E-mail ID")
obj.focus()
return false
}
if (txt.indexOf(dot, (lat + 2)) == -1) {
alert("Invalid E-mail ID")
obj.focus()
return false
}
if (txt.indexOf(" ") != -1) {
alert("Invalid E-mail ID")
obj.focus()
return false
}
}
return true
}
最佳答案
检查您的控件是否为“runat=server”
如果是,则呈现的 html 控件名称应类似于:#ctl00_ContentPlaceHolderMain_emailid。
尝试更改此设置:
var txt = document.getElementById("emailid").value;
对此:
var txt = document.getElementById('<%=emailid.ClientID %>').value;
如果控件位于内容面板内,ClientID 将为您提供全名。
在另一边,您正在访问属性 obj.focus()
,但我看不到在作用域内声明的 obj。
关于c# - 需要 Javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19356999/