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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Location
    Southampton, UK
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Newby Form Validation

    Hi,
    Using Dreamweaver cs4 on a Win7 pc.
    I am aware that form validation has probably been done to death and that the answer to my question is probably blatantly obvious, but after several hours of experimenting and searching, I've finally given up! So here goes...
    I am experimenting with form validation. I have some basic code that works, but when I try to put it into any sort of function, it stops working, eg
    here is what works:
    Code:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <HEAD>
    <TITLE>test form1</TITLE>
    
    <SCRIPT LANGUAGE="JavaScript">
    function validate() {
    	if (document.Subscribe.Name.value.length < 1) {
    	alert("Please enter a value for name");
    	setTimeout(function() {document.Subscribe.Name.focus(); }, 10);
    	return false;
    	}
           if (document.Subscribe.Email.value.length < 1) {
           alert("Please enter an Email Address.");
           return false;
           } 
        return true;
        }
    </SCRIPT>
    
    </HEAD>
     
    <BODY>
    
        <FORM name="Subscribe" Action="resultspage.asp" onSubmit="return validate();">
        name:<input type="text" name="Name"><br>
        email:<input type="text" name="Email"><br>    <input type=submit value="Submit">
        </FORM>
    
    </BODY>
    </HTML>
    If I change the code to add my isDEmpty function and call it from my validate function, then everything stops working.
    Code:
    function isDEmpty(elName, niceName){
    	if (elName.value.length < 1) {
    	alert("Please enter a value for " + niceName);
    	setTimeout(function() {elName.focus(); }, 10);
    	return true;
    	}
    }
    
    function validate() {
        if isDEmpty(document.Subscribe.Name, "Customerrrr Name") {
    		return false;
    	}
    Can anybody explain to me what I am doing wrong? I guess a basic understanding of Javascript would help, but at the moment I am just hacking about trying to get my head around the syntax and how it all goes together... you gotta start somewhere, right?

    Thanks in advance for any help.

    Dan

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    That will screw things up!

    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

    Form validation of the pattern if (document.Subscribe.Email.value.length < 1) { is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    I don't understand what your function isDempty() is supposed to do. You have already checked for an empty string.

    “The old believe everything; the middle aged suspect everything; the young know everything.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

  • #3
    New to the CF scene
    Join Date
    Jan 2010
    Location
    Southampton, UK
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Philip, thanks for taking the time to reply.

    The code that I posted is purely an experiment to try to educate my brain. I find if I can make something work then I get a better understanding, so the fact that is serves no useful purpose is kind of irrelevant at this point.

    The isDEmpty function is purely an attempt to take that part of the code out of the validate function and put it into its own function so that it is callable/reusable. But it doesn't work and I don't understand why.

    I have changed the script line as advised to <script type = "text/javascript">. I guess I will find some reading on this somewhere!
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>... have taken this line out but, my isDEmpty still doesn't work.

    Thanks again.

    Dan

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Use your error console or Firebug to identify syntax errors.

    if (isDEmpty(document.Subscribe.Name, "Customerrrr Name")) {

    But as I say a test for just an empty string is close to useless. In practice most fields require their own unique validation function which is not very re-useable because they require different patterns. (FirstName, LastName, Address, City, Email, Phone). Beware of "all-singing-all-dancing" validation scripts.
    Last edited by Philip M; 03-24-2011 at 05:29 PM.

  • Users who have thanked Philip M for this post:

    cameldan (03-24-2011)

  • #5
    New to the CF scene
    Join Date
    Jan 2010
    Location
    Southampton, UK
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Brilliant!
    I knew I wasn't far off.

    Thanks for the hints about the error console (not found it yet, but will continue looking) and Firebug <scurries off to download>.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    In IE - Tools > Internet Options > Advanced > Browsing > Check the box - Display a notification of every script error.


  •  

    Posting Permissions

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