Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Validate if the data 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 the dash character is automatically with the number example (047-222-2222).
0
codebreaker
Asked:
codebreaker
  • 2
1 Solution
 
glottisCommented:
1. you want to add underscore at the begining of the email:

in the echeck function before the line:
  var at="@"
insert this:
  if (str.charAt(0) != '_') {
    str = "_" + str;
  }

this will see if there is a underscore at the first charachter, if not then add one to it.

2. you want to add dashes in the phone:
if the phone is valid. ie 0471231234 then you coud do is:
pno.value = pno.value.substring(0, 3) + "-" + pno.value.substring(3, 3) + "-" + pno.value.substring(6)

hope this is what you want and helps :)
0
 
objectsCommented:
u should ask this question in javascript topic.
0
 
CleanupPingCommented:
codebreaker:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
glottisCommented:
i did help... even in though it was in wrong TA.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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