?
Solved

Validate if the data I typed in the textbox is a valid e-mail address

Posted on 2003-03-12
9
Medium Priority
?
174 Views
Last Modified: 2008-02-01
Im currently constructing a e-mail like JavaScript Program.
It has a FirstName, LastName, Phone and e-mail address TextBox.

==========================================================================
<script language="Javascript">
function echeck(str) {

         var at="@"
         var dot="."
         var lat=str.indexOf(at)
         var lstr=str.length
         var ldot=str.indexOf(dot)
         if (str.indexOf(at)==-1){
            alert("Invalid E-mail ID")
            return false
         }

         if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
            alert("Invalid E-mail ID")
            return false
         }

         if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
             alert("Invalid E-mail ID")
             return false
         }

          if (str.indexOf(at,(lat+1))!=-1){
             alert("Invalid E-mail ID")
             return false
          }

          if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
             alert("Invalid E-mail ID")
             return false
          }

          if (str.indexOf(dot,(lat+2))==-1){
             alert("Invalid E-mail ID")
             return false
          }
         
          if (str.indexOf(" ")!=-1){
             alert("Invalid E-mail ID")
             return false
          }

           return true                        
    }

function ValidateForm(){
    var emailID=document.frmSample.txtEmail
    var fname=document.frmSample.fn
    var lname=document.frmSample.ln
    var pno=document.frmSample.pn
   
    if ((fname.value==null)||(fname.value=="")){
         alert("Please Enter your Firstname")
         fname.focus()
         return false
    }
    if ((lname.value==null)||(lname.value=="")){
         alert("Please Enter your Lastname")
         lname.focus()
         return false
    }
    if ((pno.value==null)||(pno.value=="")){
         alert("Please Enter your Phone NUmber")
         pno.focus()
         return false
    }
    if ((emailID.value==null)||(emailID.value=="")){
         alert("Please Enter your Email ID." + "Example you@yahoo.com")
         emailID.focus()
         return false
    }
    if (echeck(emailID.value)==false){
         emailID.value=""
         emailID.focus()
         return false
    }
    return true
}
</script>
============================================================================

What I want is that after i put my e-mail add and i click submit, first it will verify the first character if it has an underscore character if not it must automatically put an underscore. also in phone number, when I type the first 3 and second 3 digits, the dash character should automatically type. Example (047-222-2222).
0
Comment
Question by:codebreaker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 14

Accepted Solution

by:
avner earned 1400 total points
ID: 8118625
To add the Dash after every 3 lines :
<html>
<head>
<title>about:blank</title>
<script language="javascript1.2">
<!-- copyright(c) avcoh@yahoo.com
function addDash(obj)
{
var sValue = obj.value;
var iLen = sValue.length;
if (iLen<3){return true;}
else if (iLen==3){     sValue+="-";}
else if (iLen<6){return true;}
else if (iLen==7){sValue+="-";}
obj.value = sValue;
}

-->
</script>
<style>

</style>
</head>
<body>
<input type="text" onkeypress="addDash(this)" maxlength="12">
</body>
</html>

0
 
LVL 14

Expert Comment

by:avner
ID: 8118634
For the underscore thing :

<html>
<head>
<title>about:blank</title>
<script language="javascript1.2">
<!-- copyright(c) avcoh@yahoo.com
function addUndersocre(obj)
{
var firstField  = obj.value.charCodeAt(0);
if (firstField != 95)
     {
          obj.value = "_"+obj.value
     }
}

-->
</script>
<style>

</style>
</head>
<body>
<input type="text" onkeypress="addUndersocre(this)" maxlength="12">
</body>
</html>
0
 

Author Comment

by:codebreaker
ID: 8133769
avner,

thanks for the codes!, you almost got it. But in the underscore in the e-mail. Once I type a letter it surely adds an underscore before it, but when I type the second letter it adds another underscore.

So in the code
============================
obj.value = "_"+obj.value
============================

I remove the "+obj.value" to avoid the adding of another underscore. Anyway, thank you very much!.

By the way in the phone number textbox, how can I restrict a letter when I typed a letter in the textbox 'cause I only want numbers to enter in the phone number textbox.

I will increase the point to 350
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 14

Expert Comment

by:avner
ID: 8145792
There shouldn't be a problem with the underscore code, I tested it again, but if fixed the problem you seems to have, then all is well.

For the digit's problem :

<html>
<head>
<title>about:blank</title>
<script language="javascript1.2">
<!-- copyright(c) avcoh@yahoo.com
function addDash(obj)
{
var sValue = obj.value;
var iLen = sValue.length;
if (iLen<3){return true;}
else if (iLen==3){     sValue+="-";}
else if (iLen<6){return true;}
else if (iLen==7){sValue+="-";}
obj.value = sValue;
}

 function  charValidation ()
               {
               var legal = false;
               var sKeyCode = event.keyCode;
          if (( sKeyCode>=48 && sKeyCode <=57) || (sKeyCode==8 || sKeyCode==9))
                    {
                         legal = true;
                    }
               else if (sKeyCode == 189)
                    {
                         legal = true;
                    }
               event.returnValue = legal;
               }
-->
</script>
<style>

</style>
</head>
<body>
<input type="text" onkeypress="addDash(this)" maxlength="12" onkeydown="charValidation()">
</body>
</html>
0
 

Author Comment

by:codebreaker
ID: 8156781
avner,

I'm sorry but there's no change, still it accepts alphabet characters.
0
 
LVL 14

Expert Comment

by:avner
ID: 8156997
Have you tested my code example (for IE) ? i re-tested it on IE5.5 and IE6 and it is fully working.
If it does not work for you then you have implemented the code wrongly, post your code and  I will look at it.

0
 

Author Comment

by:codebreaker
ID: 8157104
I'm sorry because Im using IE 4 browser. Maybe this is the reason why your codes are not working on me. I think IE4 doesnt support that kind of scripts.

These are the codes:
=====================================
<html>
<head>
<title>about:blank</title>
<script language="javascript1.2">
<!-- copyright(c) avcoh@yahoo.com
function addDash(obj)
{
var sValue = obj.value;
var iLen = sValue.length;
if (iLen<3){return true;}
else if (iLen==3){     sValue+="-";}
else if (iLen<6){return true;}
else if (iLen==7){sValue+="-";}
obj.value = sValue;
}

function  charValidation ()
              {
              var legal = false;
              var sKeyCode = event.keyCode;
         if (( sKeyCode>=48 && sKeyCode <=57) || (sKeyCode==8 || sKeyCode==9))
                   {
                        legal = true;
                   }
              else if (sKeyCode == 189)
                   {
                        legal = true;
                   }
              event.returnValue = legal;
              }
-->
</script>
<style>

</style>
</head>
<body>
<input type="text" onkeypress="addDash(this)" maxlength="12" onkeydown="charValidation()">
</body>
</html>
0
 
LVL 14

Expert Comment

by:avner
ID: 8157154
It is possible that it is not supported by IE 4 , I've no idea.

If you'll search the web you'll find that somewhere around 0.5% is the amount of clients till using IE4 , I don't think it worth development time.

Do you see any specific error when you test the script ?(look for the small JavaScript error at the bottom-left side of the status bar).


0
 
LVL 14

Expert Comment

by:avner
ID: 8157172
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question