Status
Not open for further replies.

sahil00150

Banned
Banned
1,970
2009
208
0
Hello i got assignment from my university i am not able to make it properly doing some mistakes well got this assignment

Code:
[B]Make a form in HTML document containing only one field named as Email and one Submit Button. Write your own JavaScript function to validate the email address.

 

The Validation should be as follows:

 
It must accept only your VU Student email address i.e; it must not accept any other student’s VU email address.
It must show alert on acceptance or denial of the email address after validation.
The name of the function should contain your student ID like CheckEmail_BC102034568.
If you enter your wrong student email id then it must show alert as well.
It must accept your student ID only at vu.edu.pk, means if you enter BC102034568@hotmail.com then it must not accept it.
There must be unique alerts for each error; like if the length of the student ID is larger or smaller than the original ID then the alert should be according to that.
During each alert, the entered email address must also be shown in the alert box like: “BC102034560000008@vu.edu.pk is not your Valid Email address”[/B]

i made this

Code:
 [B]<html>
<head>
<script type="text/javascript">
function CheckMyEmail()
{
var x=document.forms["myForm"]["email"].value;
var value=("BC102034568");
var at=x.indexOf("@");
var dot=x.lastIndexOf("vu.edu.pk");
if (at<1 || dot<at+2 || dot+2>=x.length)
{
alert("This is Not your valid VU e-mail address");
return false;
}
else
{
alert("This is your valid VU e-mail address");
}
}
</script>
</head>

<body>
<form name="myForm" action="example.html" onsubmit="return CheckMyEmail();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>[/B]

confused i am stuck what to do now.......anyone Help?

In this whole program
if i put BC102034568@vu.edu.pk in user input text field the alert box show me valid email and if put another email like wj@gmail.com, wj@edu.pk, the alert box must show not valid address.
 
Last edited:
16 comments
The problem of not showing it as valid: BC102034568@vu.edu.pk is at :
PHP:
if (at<1 || dot<at+2 || dot+2>=x.length)
This works:
PHP:
if (at<1 || dot<at+1 || dot+2>=x.length)
But there are still few other checks you need to implement.
 
How about using regex for this? I'm not sure if it will be appreciated by your teacher (since you're not implementing an algorithm actually rather you're leaving the parsing job to the language..). Anyway, the following pattern should work

PHP:
/([A-Z0-9._%+-]+)@vu.edu.pk/i

Edit it according to your needs.. (set the max length and all..)

Regards,
Gaurav
 
Code:
 <html>
<head>
<script type="text/javascript">
function CheckMyEmail()
{
var x=document.forms["myForm"]["email"].value;
var value=("BC102034568");
var at=x.indexOf("@");
var dot=x.lastIndexOf("vu.edu.pk");
if (at<1 || dot<at+1 || dot+2>=x.length)
{
alert("This is Not your valid VU e-mail address");
return false;
}
else
{
    if(x="BC102034568@vu.edu.pk")
        {
        alert("This is your valid VU e-mail address");
        }
}
}
</script>
</head>

<body>
<form name="myForm" action="example.html" onsubmit="return CheckMyEmail();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>
 
Code:
 <html>
<head>
<script type="text/javascript">
function CheckMyEmail()
{
var x=document.forms["myForm"]["email"].value;
var value=("BC102034568");
var at=x.indexOf("@");
var dot=x.lastIndexOf("vu.edu.pk");
if (at<1 || dot<at+1 || dot+2>=x.length)
{
alert("This is Not your valid VU e-mail address");
return false;
}
else
{
    if(x="BC102034568@vu.edu.pk")
        {
        alert("This is your valid VU e-mail address");
        }
}
}
</script>
</head>

<body>
<form name="myForm" action="example.html" onsubmit="return CheckMyEmail();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>

Thanks buddy but when i put xyz@vu.edu.pk it show me valid address. i only wanna make BC102034568@vu.edu.pk valid address all other values and email address must be show invalid.
 
Must be a typo, but he is using assignment operator which forcefully sets value:
Code:
if(x="BC102034568@vu.edu.pk")
This:
Code:
if(x==="BC102034568@vu.edu.pk")
Also, make sure to return false if it doesn't matches else it will submit the form.
 
thanks i done it

Code:
[B]<html>
<head>
<script type="text/javascript">
function CheckMyEmail()
{
var x=document.forms["myForm"]["email"].value;
var value=("BC102034568");
var at=x.indexOf("@");
var dot=x.lastIndexOf("vu.edu.pk");
if (at<1 || dot<at+1 || dot+2>=x.length)
{
alert("This is Not your valid VU e-mail address");
return false;
}
else
{
    if(x=="BC102034568@vu.edu.pk")
        {
        alert("This is your valid VU e-mail address");
        return false;
        }
else
{
alert("not an valid email address");
return false;
}
}
}
</script>
</head>

<body>
<form name="myForm" action="example.html" onsubmit="return CheckMyEmail();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>[/B]

now i wanna add some more function it it the alert box

There must be unique alerts for each error; like if the length of the student ID is larger or smaller than the original ID then the alert should be according to that.
 
Last edited:
glad u got it :) I used my php knowledge, becoz basically I dunno Java ! :(

from alert(x + 'this .....') u can get the email which is entered in the box
 
i never tried reg ex
The only thing which is remain is character counting how much letter submitted in input box.

alert box must show counted characters + valid or invalid email + message



Code:
[B]<html>
<head>
<script type="text/javascript">
function CheckMyEmail()
{
var x=document.forms["myForm"]["email"].value;
var value=("BC102034568");
var at=x.indexOf("@");
var dot=x.lastIndexOf("vu.edu.pk");
if (at<1 || dot<at+1 || dot+2>=x.length)
{
alert( x + " is Not your valid VU e-mail address");
return false;
}
else
{
    if(x=="BC102034568@vu.edu.pk")
        {
        alert( x + " is your valid VU e-mail address");
        return false;
        }
else
{
alert( x + " is not an valid email address");
return false;
}
}
}
</script>
</head>

<body>
<form name="myForm" action="example.html" onsubmit="return CheckMyEmail();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>[/B]

you again forgot to make return function well i done it.

if someone submit more then 40 characters email the alert box show email it too much large of some one enter minimum characters like 2 or 3 character alertbox must show email characters are less.
 
Last edited:
thanks buddy for your idea i am trying it my self alot actually i love PHP and have knowledge about PHP, i totally forget javascript thats why i am asking here for help.
 
i never tried reg ex
The only thing which is remain is character counting how much letter submitted in input box.

alert box must show counted characters + valid or invalid email + message



Code:
[B]<html>
<head>
<script type="text/javascript">
function CheckMyEmail()
{
var x=document.forms["myForm"]["email"].value;
var value=("BC102034568");
var at=x.indexOf("@");
var dot=x.lastIndexOf("vu.edu.pk");
if (at<1 || dot<at+1 || dot+2>=x.length)
{
alert( x + " is Not your valid VU e-mail address");
return false;
}
else
{
    if(x=="BC102034568@vu.edu.pk")
        {
        alert( x + " is your valid VU e-mail address");
        return false;
        }
else
{
alert( x + " is not an valid email address");
return false;
}
}
}
</script>
</head>

<body>
<form name="myForm" action="example.html" onsubmit="return CheckMyEmail();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>[/B]
you again forgot to make return function well i done it.

if someone submit more then 40 characters email the alert box show email it too much large of some one enter minimum characters like 2 or 3 character alertbox must show email characters are less.
PHP:
if (x.length >= 40) {
alert("Too much");
} else if(x.length =< 3) {
//ahh;
alert("Too Less");
} else {
//ok;
}
 
but how i can implement your code in fields ?

Code:
[B]if (x.length >= 40) {
alert("Too much");
} else if(x.length =< 3) {
//ahh;
alert("Too Less");
} else {
//ok;
} [/B]
 
Before checking any other checks
PHP:
<html>
<head>
<script type="text/javascript">
function CheckMyEmail()
{
var x=document.forms["myForm"]["email"].value;

if (x.length >= 40) {
alert("Too much");
} else if(x.length =< 3) {
//ahh;
alert("Too Less");
} else {
//ok;
var value=("BC102034568");
var at=x.indexOf("@");
var dot=x.lastIndexOf("vu.edu.pk");
if (at<1 || dot<at+1 || dot+2>=x.length)
{
alert( x + " is Not your valid VU e-mail address");
}
else
{
    if(x=="BC102034568@vu.edu.pk")
        {
        alert( x + " is your valid VU e-mail address");
        return true;
        }
else
{
alert( x + " is not an valid email address");
}
}
}
return false;
} 

</script>
</head>

<body>
<form name="myForm" action="example.html" onsubmit="return CheckMyEmail();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>
:|
 
Ahh, yeah, a small type in the comparison operator: =<
PHP:
<html>
<head>
<script type="text/javascript">
function CheckMyEmail()
{
var x=document.forms["myForm"]["email"].value;

if (x.length >= 40) {
alert("Too much");
} else if(x.length <= 3) {
alert("Too Less");
} else {
//ok;
var value=("BC102034568");
var at=x.indexOf("@");
var dot=x.lastIndexOf("vu.edu.pk");
if (at<1 || dot<at+1 || dot+2>=x.length)
{
alert( x + " is Not your valid VU e-mail address");
}
else
{
    if(x=="BC102034568@vu.edu.pk")
        {
        alert( x + " is your valid VU e-mail address");
        return true;
        }
else
{
alert( x + " is not an valid email address");
}
}
}
return false;
} 

</script>
</head>

<body>
<form name="myForm" action="example.html" onsubmit="return CheckMyEmail();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>
 
Status
Not open for further replies.
Back
Top