|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
JScript - General - Question - How to call the javascript
I am trying to call javascript from html (asp) code. So if my javascript function return true then it will display Welcome User and if return false then it will display Welcome Guest. Now for this what code should I write in HTML. Is it possible through Response.Write?
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function IsEmailValid(value) {
if (value == '')
{
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Welcome <% Response.Write(IsEmailValid('')) %>
</div>
</form>
</body>
</html>
|
|
#2
|
||||
|
||||
|
Hi, and welcome to the forums. Is there a reason why you need to use javascript for this? You could use a serverside script to do this, eg:
Code:
<%
Function isValidEmail(myEmail)
dim isValidE
dim regEx
isValidE = True
set regEx = New RegExp
regEx.IgnoreCase = False
regEx.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
isValidE = regEx.Test(myEmail)
'isValidEmail = isValidE
If isValidE Then
isValidEmail = "User"
Else
isValidEmail = "Guest"
End If
End Function
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Welcome <%=isValidEmail("test@test.com")%>
<br>
Welcome <%=isValidEmail("testtest.com")%>
</div>
</form>
</body>
</html>
|
|
#3
|
|||
|
|||
|
I want to pass recordset value to javascript function. So I have use following code. But value doesn't get pass to the javascript function
Code:
Response.Write "<script type='text/javascript'> document.getElementById('result1').value=IsEmailVa lid('" & rsResultSet("email") & "');</script>"
|
|
#4
|
|||
|
|||
|
Thanks for reply
I have used following code. But javascript is not working. Recordset value is not getting passed Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "(URL address blocked: See forum rules)">
<html xmlns="(URL address blocked: See forum rules)">
<head>
<title></title>
<script type="text/javascript" >
function validateEmail(email) {
var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (email.match(emailRegEx)) {
return true;
} else {
return false;
}
}
function IsEmailValid(email) {
alert(email);
if (validateEmail(email) == false) {
return 0;
}
var myReq = new XMLHttpRequest();
if (window.XMLHttpRequest) {
var url = "(URL address blocked: See forum rules)=" + email;
myReq.open("GET", url, false)
myReq.send();
var result = myReq.responseXML.text;
if (result == "true") {
return "1";
}
else {
return "0";
}
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<input type="text" id="result1" name="result1" value="" />
<input type="text" id="text1" name="text1" />
<input type="submit" id="btnDisplay" value="Display Valid Emails" />
<div>
Welcome
<%
response.write "<script type='text/javascript'> document.getElementById('result1').value=IsEmailVa lid('" & myRecordSet("GGG") & "');</script>"
if Request.Item("result1") = "1" or Request.Item("result1") = "" then
Response.Write("<input value=""" & "TEST1" & """ name=""email" & "1""" & " id=""Text"" style=""background-color:red"" />")
else
Response.Write("<input value=""" & "TEST2" & """ name=""email" & "1""" & " id=""Text1"" />")
end if
%>
</div>
</form>
</body>
</html>
Last edited by gkhanna : November 2nd, 2009 at 01:48 PM. Reason: some change |
|
#5
|
||||
|
||||
|
the problem is not what you think.. the value is passed however when
the code run, there is no element yet. you should use the onload event, so try this code and it should work fine: Code:
<script type="text/javascript">
var sInitialEmail = "<%=rsResultSet("email")%>";
window.onload = function WindowLoad(event) {
document.getElementById("result1").value = IsEmailValid(sInitialEmail);
}
</script>
|
|
#6
|
||||
|
||||
|
I dont understand why you need to use javascript, did you try my serverside example?
|
|
#7
|
||||
|
||||
|
Threads Merged. Please try and keep all related content in the same thread.
|
|
#8
|
||||
|
||||
|
Quote:
same code twice, use what he already got. fine with me. ![]() |
|
#9
|
||||
|
||||
|
Quote:
I agree. The reason I suggested a serverside script was that the original post asked how to display "Welcome User" or "Welcome Guest" when a user browsed to a page, which could be done with serverside script. As you say though, if the OP already has the client-side script to validate email he may as well reuse this code! |
![]() |
| Viewing: ASP Free Forums > Programming > ASP Development > JScript - General - Question - How to call the javascript |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|