Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying a Substring of a String 2

Status
Not open for further replies.

tyt2000

MIS
Mar 14, 2005
7
SA
Hi,

This is my first time using Java code and I'd really appriciate it if someone helped me with this little problem. I have a registration form that has a Java script at the top to validate fields, I did the code for validating blanks and a verify password. There is one piece of code that I need, I want the "email" field to accept only e-mails that end with "@ut.edu". I've searched the net but couldn't find anything helpful. Here is the code for the entire page:

<BASEFONT FACE="Comic Sans MS" COLOR="DarkBlue">
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function VerifyData()
{

if (document.frmUser.Password.value != document.frmUser.VerifyPassword.value)
{
alert ("Your passwords do not match - please reenter");
return false;
}

if (document.frmUser.email.value == "")
{
alert ("You must enter an E-Mail Address");
return false;
}

if (document.frmUser.FirstName.value == "")
{
alert ("You must enter your First Name");
return false;
}

if (document.frmUser.LastName.value == "")
{
alert ("You must enter your Last Name");
return false;
}

if (document.frmUser.Password.value == "")
{
alert ("You must enter a Password");
return false;
}
else
return true;


}

-->

</SCRIPT>

<TITLE>Used College Books System - User Registration</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFF80">
<CENTER>
<%
If Request("Update") = "True" Then
Response.Write "<H1>Used College Books System<BR> Update User Registration</H1>"
Else
Response.Write "<H1>Used College Books System<BR> New User Registration</H1>"
End If
%>
</CENTER>
<P>

<%
If Request("Update") = "True" Then
Response.Write "Please change your registration information as listed below<P>"
Else
If Request("NotFound") = "True" Then
Response.Write "<I>We were unable to locate your information. " & _
"Please take the time to register again.</I><P>"
Else
Response.Write "<CENTER>(If you're already registered with us, " & _
"then click the 'Login' link below.)</CENTER><P>"
End If
Response.Write "You only need to register with our system if you want to " & _
"purchase an existing book, or sell your own books " & _
"on these pages. <BR> " & _
"In order to use these services, please take a few minutes " & _
"to complete the form below. Once you have done that, " & _
"you will have full access to the system."
End If
%>

<FORM ACTION="AddUser.asp" NAME="frmUser" METHOD="POST"
onSubmit="return VerifyData()">
<TABLE BORDER=0>
<TR>
<TD WIDTH=20% ROWSPAN=11>&nbsp;</TD>
<TD>E-Mail Address:</TD>
<TD><INPUT TYPE="Text" NAME="email" VALUE="<%= Session("EMailAddress")%>"
SIZE="40"></TD>
</TR>
<TR>
<TD>First Name:</TD>
<TD><INPUT TYPE="Text" NAME="FirstName" VALUE="<%= Session("FirstName")%>"
SIZE="40"></TD>
</TR>
<TR>
<TD>Last Name:</TD>
<TD><INPUT TYPE="Text" NAME="LastName" VALUE="<%= Session("LastName")%>"
SIZE="40"></TD>
</TR>
<TR>
<TD>Phone Number:</TD>
<TD><INPUT TYPE="Text" NAME="PhoneNumber" VALUE="<%= Session("PhoneNumber")%>"
SIZE="40"></TD>
</TR>
<TR>
<TD>&nbsp;<P>Password:</TD>
<TD VALIGN=bottom><INPUT TYPE="Password" NAME="Password"
VALUE="<%= Session("Password") %>" SIZE="40"></TD>
</TR>
<TR>
<TD>Verify Password:</TD>
<TD><INPUT TYPE="Password" NAME="VerifyPassword" SIZE="40"></TD>
</TR>
<TR>
<TD></TD>
<TD ALIGN=CENTER COLSPAN=2><BR>
<INPUT TYPE="Submit" VALUE="Submit Registration">
&nbsp;&nbsp;<INPUT TYPE="RESET"></TD>
</TR>
</TABLE>
</FORM>

<HR>
<TABLE BORDER=0 WIDTH=100%>
<TR ALIGN=CENTER>
<TD WIDTH=25%><A HREF="BrowseListings.asp">Browse the listings</A></TD>
<TD WIDTH=25%><A HREF="Login.asp">Login</A></TD>
<TD WIDTH=25%>Register</TD>
<TD WIDTH=25%><A HREF="default.asp">Log-Out</A></TD>
</TR>
</TABLE>

</BODY>
</HTML>

Thanks a lot for your time!

- Thamer
 

This should work for you:

Code:
<html>
<head>
	<script type="text/javascript">
		var myEmail1 = 'abc@ut.edu';
		var myEmail2 = 'abc@ut.eda';

		checkEmail(myEmail1);
		checkEmail(myEmail2);

		function checkEmail(address) {
			if (address.lastIndexOf('@ut.edu') != address.length - '@ut.edu'.length) alert('Address ' + address + ' is invalid!');
		}
	</script>
</head>
<body></body>
</html>

Hope this helps,
Dan


The answers you get are only as good as the information you give!

 
Hi there,

Code:
 var a =new String(document.frmUser.email.value);
 if ( a.substring(a.length -7,a.length)!="@ut.edu" )
 {
   alert("do not finish with @ut.edu");
   return false;
 }


Ok ?
 
Notice that the number "7" is the number of characters you want to find at the end of the 'email'.
 
Dan,

Thanks a lot for the quick response, it was very helpful. I had to modify it to make it work:

Code:
var myEmail1 = document.frmUser.email.value;     
checkEmail(myEmail1);
        
function checkEmail(address) {
if (address.lastIndexOf('@ut.edu') != address.length - '@ut.edu'.length) alert('Address ' + address + ' is invalid! You must enter a UT E-mail Address');
}

It works, but the problem is that it still redirects me to the next page when I click "ok" on the message box, I want it to stay on the same page so the user can enter a valid e-mail, just like what happens when I click "ok" on the other message boxes that validate the other fields. Any idea how I could make that happen?

Congrats on becoming TipMaster of the Week!

Thanks again,
- Thamer
 
bclt,

Thanks a lot for that piece of code, this one works too, and forces the users to re-enter the e-mail instead of redirecting them to the next page. I really appriciate your help! now I know how it works...

Although I'd still like to know why Dan's piece of code didn't force the user to re-enter the e-mail. I'm trying to learn :)

Thanks!

- Thamer
 

I'd still like to know why Dan's piece of code didn't force the user to re-enter the e-mail.

Because my code was only to show you how to do the verification you asked for. It did not do anything else - I assumed you would integrate the check you asked for yourself.

Hope this clarifies things,
Dan


The answers you get are only as good as the information you give!

 
Hope this clarifies things

Definitely, thanks for the clarification, I should have included more info in my post (after reading your signature, lol).

- Thamer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top