• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

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

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
codebreaker
Asked:
codebreaker
  • 6
  • 3
1 Solution
 
avnerCommented:
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
 
avnerCommented:
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
 
codebreakerAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
avnerCommented:
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
 
codebreakerAuthor Commented:
avner,

I'm sorry but there's no change, still it accepts alphabet characters.
0
 
avnerCommented:
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
 
codebreakerAuthor Commented:
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
 
avnerCommented:
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
 
avnerCommented:
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now