473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Javascript:Script not working

Hi all,
Executed the following javascript code through localhost...but the script is not working at all.Is the syntax of indexOf correct?
The code does the following..checks if a valid email id is entered or not.If a non valid email id is entered,the first part of 'if' statement is executed else the latter part of 'if' statement is executed.



Regards
Swetha



[HTML]<html>
<head>
<script language="javascript">
function email_check(thisform)
{

apos=thisform.email.indexOf("@")
dpos=thisform.email.lastIndexOf(".")
if (apos<1||dpos-apos<2)
{
alert("Please Enter valid Email Id")
thisform.email.focus()
return false
}
else
{
alert("Valid Email Entered")
return true

}
}
</script>
</head>


<body>

<form name="validate" method="post" onsubmit="return email_check(this)" >
&nbsp;<p>&nbsp;</p>
<p>&nbsp;</p>
Email:<input type="text" size="29" name="email"></p>
<input type="submit" value="submit" > </p>
</form>
</body>
</html>[/HTML]
Jul 23 '07 #1
2 1320
Expand|Select|Wrap|Line Numbers
  1. function echeck(str) {
  2.  
  3.                 var at="@"
  4.                 var dot="."
  5.                 var lat=str.indexOf(at)
  6.                 var lstr=str.length
  7.                 var ldot=str.indexOf(dot)
  8.                 if (str.indexOf(at)==-1){
  9.                 alert("Invalid E-mail ID")
  10.                 return false
  11.                 }
  12.  
  13.                 if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
  14.                 alert("Invalid E-mail ID")
  15.                 return false
  16.                 }
  17.  
  18.                 if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
  19.                     alert("Invalid E-mail ID")
  20.                     return false
  21.                 }
  22.  
  23.                 if (str.indexOf(at,(lat+1))!=-1){
  24.                     alert("Invalid E-mail ID")
  25.                     return false
  26.                 }
  27.  
  28.                 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
  29.                     alert("Invalid E-mail ID")
  30.                     return false
  31.                 }
  32.  
  33.                 if (str.indexOf(dot,(lat+2))==-1){
  34.                     alert("Invalid E-mail ID")
  35.                     return false
  36.                 }
  37.  
  38.                 if (str.indexOf(" ")!=-1){
  39.                     alert("Invalid E-mail ID")
  40.                     return false
  41.                 }
  42.  
  43.                  return true                    
  44.             }
YOU CAN USE THIS FUNCTION

FOLLOWED BY

Expand|Select|Wrap|Line Numbers
  1. function ValidateFields() {
  2. var emailID = document.all('txtMail').value;
  3.                             if (document.all('txtMail').value.length < 1){
  4.                             alert("Please Enter your Email ID");
  5.                             document.all('txtMail').value = "";
  6.                             document.all('txtMail').focus();        
  7.                             return false; 
  8.                         }
  9.                         if (echeck(emailID) == false){
  10.                             document.all('txtMail').value = "";
  11.                             document.all('txtMail').focus();        
  12.                             return false;
  13.                         }
  14.  
  15.     return true;        
  16.             }
LASTLY U CAN USE IN ONCLICK
Expand|Select|Wrap|Line Numbers
  1. (onclick") = "return ValidateFields();"
FINISH
Jul 23 '07 #2
gits
5,390 Expert Mod 4TB
Hi all,
Executed the following javascript code through localhost...but the script is not working at all.Is the syntax of indexOf correct?
The code does the following..checks if a valid email id is entered or not.If a non valid email id is entered,the first part of 'if' statement is executed else the latter part of 'if' statement is executed.



Regards
Swetha
hi ...

have a close look at your apos- and dpos- lines. you have to process the value of the email-field, so adapt the lines the following way and it will work:

Expand|Select|Wrap|Line Numbers
  1. var apos = thisform.email.value.indexOf("@");
  2. var dpos = thisform.email.value.lastIndexOf(".");
and don't forget the var-declaration its not strict but always good practice ;)

kind regards
Jul 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: noah.coffey | last post by:
Hello, I'm wanting to do something and I'm not sure if it's possible (I'd sure like it to be). I'm using a neat service called FeedSweep to aggregate a large list of XML/RSS/ATOM feeds. The...
1
by: Jack Whitton | last post by:
Hi Im creating a website at the minute and I need some help with the homepage. I want there to be 3 flags, English German and Spanish, and when a user clicks on there flag I want them to be...
6
by: nate | last post by:
Hello, Does anyone know where I can find an ASP server side script written in JavaScript to parse text fields from a form method='POST' using enctype='multipart/form-data'? I'd also like it to...
1
by: 2007boy | last post by:
I am puzzled by a problem I am having with a javascript that is working on my computer (mac osx) but fail when put online. it happens only with the old IE 5.2 for mac and the iCab browsers. the code...
1
by: sarwarmohiuddin | last post by:
Hello MAtes, I am having this problem of adding a script element in a rich text editor box. i am doing this. The problem is that it won't add the script tag alltogather, or give an error "Unknown...
6
by: ApOG | last post by:
Hello everyone, I have this javascript code working perfectly with IE, but with firefox nothing happens when running the function... function add_div_field () { var ni =...
1
by: jshokoo | last post by:
i am having problems with my script which I have embedded in a jsp file.I am invoking the funtion on submitting the form but no call is being made to the function,instead the is an error message...
2
by: shirlene | last post by:
hihi....encountered an error with an web application... is this related to javascript error? Line: 99 Char: 4 Error: Object doesn't support this property or method Code: 0 URL:...
4
by: TriAdmin | last post by:
I am trying to debug a client's Javascript inside a php page. It runs fine in Firefox but throws an error is IE, "can not open site,... operation aborted." The code starts like this: HTML...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.