Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check email function not working...?

    Hi,

    I'm using this two part validation method which check a number of fields with one main function and multiple minor functions. At the moment, I'm trying to validate an email address.

    The first bit of code I know works because other fields (commented below) are checked correctly. This is the main function:

    Code:
    function checkForm(formname, submitbutton)
    {
    	var errors = '';
    	//errors += checkText(formname, 'Name', 'Please enter your name');
    	errors += checkEmail(formname, 'Email', 'Please enter your email address');
    	checkThisForm(formname, submitbutton, errors);
    }
    So, it appears that this is the bit of code that isn't working:

    Code:
    function checkEmail(formname, textboxname, displaytext)
    {
    	var valid = "^.+\@(\[?)[-a-zA-Z0-9\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$";
    	var localerror = '';
    	if(!isValid(Trim(eval('document.'+formname+'.'+textboxname+'.value')), valid))
    	{
    		localerror =  '- '+displaytext+'\n';
    	}
    	else localerror = '';
    	return localerror;
    }
    It's either a problem with line 3 or line 5 as far as I can see. Can someone help me by pointing out what's wrong with the second part of the code?

    Thanks...
    Last edited by gerb; 06-05-2006 at 12:42 AM.

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Please show us the code for isValid() and Trim().

    Don't use eval() -- you don't need it. Change
    Code:
    eval('document.'+formname+'.'+textboxname+'.value')
    to
    Code:
    document.forms[formname].elements[textboxname].value
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    New Coder
    Join Date
    Dec 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks - hope you can help. Here's the code for those functions:

    Code:
    function Trim(s) 
    {
      while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
      {
        s = s.substring(1,s.length);
      }
      while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
      {
        s = s.substring(0,s.length-1);
      }
      return s;
    }
    
    function isValid(string,allowed)
    {
    //	var valid = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // define valid characters
        for (var i=0; i< string.length; i++)
    	{
    		if (allowed.indexOf(string.charAt(i)) == -1) return false;
        }
        return true;
    }

  • #4
    New Coder
    Join Date
    Dec 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?

  • #5
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your code doesn't make sense -- you're passing a RegExp to isValid(), but it only checks to see if a character is in one string that is not in a string containing all allowed characters.

    Also, Trim() could be written much simpler if you just used a RegExp.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #6
    New Coder
    Join Date
    Dec 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yep - not my code. It's a script I've found on the internet and, with my limited knowledge am trying to adapt...

    So I assume that there's no easy fix?

  • #7
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah. Trying to combine scripts without understanding how they work.

    Easy is relative. Someone who is experienced, like I am, could rewrite the code to do what you want without much trouble. On the other hand, someone who doesn't know JavaScript very well would have significantly more difficulty.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •