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 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    36
    Thanks
    18
    Thanked 0 Times in 0 Posts

    validation problem ( probably a simple fix I am missing)

    I am having a problem with the last function on this page:textcheck(). It is supposed to alert the user if no text is entered. Can anyone see what it is I have done wrong? Thanks for the assistance.

    Here is the HTML:
    Code:
    <!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" xml:lang="en" lang="en">
    
    <style>
        #title { font-weight: bolder;
                font-size: 3em;}
        
        label { font-weight: bolder;}
        
    </style>
    
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    	<meta name="author" content="DIANE PITTS" />
    
    	<title>Assignment 8 & 9 </title>
    </head>
    
    <body>
    <center><p id = "title">Assignment 8 & 9 </center></p>
    
    <div id = "content" align = "center" >
    
    <!-- Random Quote-->
    <label>Random Quote <span id= "quotes"></span></label>
    <script language="JavaScript">
    
    var Quotation=new Array() 
    
    
    Quotation[0] = "Don’t be so humble - you are not that great.";
    Quotation[1] = "The average person thinks he isn’t.";
    Quotation[2] = "Show me a sane man and I will cure him for you.";
    Quotation[3] = "If I were two-faced, would I be wearing this one?";
    Quotation[4] = "I have an existential map; it has ‘you are here’ written all over it.";
    
    var Quote = Quotation.length;
    var whichQuotation=Math.round(Math.random()*(Quote-1));
    function showQuotation(){document.write(Quotation[whichQuotation]);}
    showQuotation();
    
    </script>
    
    
    <!-- Date and date parsed-->
    
    <br /><br />
    <label>Date: </label><span id="display"></span><br />
    <label>Enter Days to add:</label>
    <input type=text id="DaysToAdd" name="DaysToAdd" size=5 value=15>
    <input type=button value="Change Date" onclick="AddDays()"><br />
    
    <label>Parsed Date:</label>
    <span id="parsedisplay"> </span><br />
    
    
    <label>Enter Time (ms):</label><input type = text value=654487454652 id="chgtime"/>
                                        <input type=button value="Change Time" onclick="ms()" ><br />
                                        
                                        
                                        
                                        
    <label>Enter UTC Date:</label><span>
    <input type = "text" value="25"  id="chgutc"/>
    <input type="button" value="Change to UTC"  onclick="utc()"/></span><br />
    
    <br /><br />
    <hr />
    </div>
    <br /><br />
    
    
    <!-- chg integer-->
    <div id = "chginteger" align="center">
    
        <label>Ceiling: </label><span id="ceiling"></span><br />
        <label>Floor: </label><span id="floor"></span><br />
        <label>Round: </label><span id="round"></span><br />
        <label>Enter Integer:</label><span><input type = "text" value="13.9" id="num"/>
                                            <input type="button" value="Change the Integer" onclick="chginteger()"/></span>
    <br /><br /><br />
    
    <hr />
    </div>
    
    <!-- Reg Expressions to check strings, url, email, phone#, date-->
    <div id = "textcheck" align = "center" >
    
    <label>Enter Sentence:</label><input type = "text" value="Enter a sentence with 'the' in it" id="newsentence"/>
                                    <input type="button" value="Fix sentence"  onclick="fixsentence()"/>
                                    <br /><br /><span id="sentence"></span><br />
    
    
    <label>Enter URL:</label><input type = "text" id="url" value="http://www.cnn.com"/>
                            <input type="button" value="Check URL" onclick="checkurl()"/>
                            <span id="ckurl"></span><br />
    
    <label>Enter email address:</label><input type = "text" id="email" value="johndoe@url.com"/>
                            <input type="button" value="Check email" onclick="checkemail()"/>
                            <span id="ckemail"></span><br />
    
    
    <label>Enter Phone Number:</label><input type = "text" value="(850)123-4567" id="cknum"/>
                                    <input type="button" value="Check Number" onclick="ckphone()"/>
                                    <span id="ckphone"></span><br />
    
    <label>Enter Date:</label><input type = "text" value="01-01-2011" id="date"/>
                            <input type="button" value="Check Date" onclick="ckdate()" />
                            <span id="ckdate"></span><br />
    <br /><br />
    <hr />
    </div>
    <!-- Reg Expressions to form field data filled out correctly-->
    <br /><br />
    <div id = "validation" align = "center" >
    <form >
    <label>Validation Area </label><br />
    <br />
    
        <!--check checkbox for checkmark and if no checkmark exists alert user to enter checkmark.-->
            <label>Are you online?</label><span><input type = "checkbox" value="" name="check" /></span><br />
            
        <!--check dropbox for selection and if no selection exists alert user to enter choice.-->
            <label>Choose<select name="droplist" id = optlist">
            <option selected="selected" value="none">none</option>
            <option value="male">male</option>
            <option value="female">female</option>
            <option value="other">other</option> </select>
            
            <br />
        <!--check name for text and if no text exists alert user to enter name.-->
            <label> Name</label><span><input name="myname" type = "text" value="" id="myname"/></span><br />
            
            
        <!--check textarea for text and if no text exists alert user to enter text.-->
            <label>Describe Something</label><br /><span><textarea cols="40" rows="5" name="mytext" id="mytext">
            
            </textarea>
            <br />
            <input type="button" value="Validate" onclick = "ValidateForm(this.form)" /></span><br />
    <br /><br /></form>
    </div>
    <br /><br />
    <script type="text/javascript" src="chapter8_9.js"></script>
    </body>
    </html>
    Here is the external javascript:
    Code:
    // date section
    
    var date = new Date();
    
    
    
    var f_box = document.getElementById("DaysToAdd");
    f_box.focus();
    
    
     var newdate=new Date();
     
     // adds a user entered number of days to the present day and give result as well as parsed result
    function AddDays() {
        DaysToAdd=document.getElementById("DaysToAdd").value;
        newdate.setDate(DaysToAdd);		
        document.getElementById("display").innerHTML = newdate.toString() ;
        document.getElementById("parsedisplay").innerHTML=Date.parse(newdate);
     }
    
    // adds a user defined milliseconds to the present day and gives result as well as parsed result
    function ms(){
        mili= parseFloat(document.getElementById("chgtime").value);
        newdate.setMilliseconds(mili);
        document.getElementById("display").innerHTML=newdate.toString();
        document.getElementById("parsedisplay").innerHTML=Date.parse(newdate);
     }
     
     // adds a user defined number of days to the present day and gives result as well as parsed result UTC
    function utc(){
        getutcdays= document.getElementById("chgutc").value;
        newdate.setUTCDate(getutcdays);
        document.getElementById("display").innerHTML=newdate.toUTCString();
        document.getElementById("parsedisplay").innerHTML=Date.parse(newdate);
    }
    
    
    function chginteger(){
        newnum=document.getElementById("num").value;
        var ceiling = Math.ceil(parseFloat(newnum));
        var floors = Math.floor(newnum);
        var rnd= Math.round(newnum);
        
        document.getElementById("ceiling").innerHTML= ceiling;
        document.getElementById("floor").innerHTML= floors;
        document.getElementById("round").innerHTML= rnd;
        
    }
    
    function fixsentence(){
        newword= "eht";
        var oldsentence = document.getElementById("newsentence").value;
        var newsentence = oldsentence.replace("the",newword);
        document.getElementById("sentence").innerHTML= newsentence;
        }
    
    function checkurl()
    {
         var theurl=document.getElementById("url").value;
         matches= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
         
         if (theurl.match(matches)){
             document.getElementById("ckurl").innerHTML="OK";
             
         }
         else
         {
             document.getElementById("ckurl").innerHTML="BAD";
             
         }
    }
    
    function ckphone()
    {
         var phoneno=document.getElementById("cknum").value;
         matches= /^\(?(\d{3})\)?[- .]?(\d{3})[- .]?(\d{4})$/;
         
         if (phoneno.match(matches)){
             document.getElementById("ckphone").innerHTML="OK";
             
         }
         else
         {
             document.getElementById("ckphone").innerHTML="BAD";
             
         }
    }
    
    function ckdate()
    {
         var date=document.getElementById("date").value;
        // document.write("I am here " + theurl);
         matches= /\b\d{1,2}[ .\/-]?\d{1,2}[ .\/-]?\d{4}\b/;
         
             if (date.match(matches)){
             document.getElementById("ckdate").innerHTML="OK";
             
         }
         else
         {
             document.getElementById("ckdate").innerHTML="BAD";
             
         }
    }
    
    
    function checkemail()
    {
         var email=document.getElementById("email").value;
        // document.write("I am here " + theurl);
         matches= /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/; 
         
             if (email.match(matches)){
             document.getElementById("ckemail").innerHTML="OK";
         }
         else
         {
             document.getElementById("ckemail").innerHTML="BAD";   
         }
    }
    
    // validates checkbox, droplist, name, and textarea when button is clicked.
    function ValidateForm(form){
        //check checkbox for checkmark and if no checkmark exists alert user to enter checkmark
        function checkbox(){
            
            ErrorText= ""; 
            if ( form.check.checked == false ) { alert ( "Please check box." ); return false; } 
            if (ErrorText= "") { form.submit() }  
    } 
        //check dropbox for selection and if no selection exists alert user to enter choice
        function droplist(){ 
            ErrorText= ""; 
            if ( form.droplist.selectedIndex == 0 ) { alert ( "Please select from droplist." ); 
                return false; 
                } 
            if (ErrorText= "") { form.submit() } 
    } 
        //check name for text and if no text exists alert user to enter name.
        function namecheck(){
            var mname=document.getElementById("myname").value;
            ErrorText= "";
            if (mname ==""){
                alert("No Name !");
                return false;
            }
            if (ErrorText= "") { form.submit() }
    }
    
        //check textarea for text and if no text exists alert user to enter text.
        function textcheck(){
            
            var mtext=document.getElementById("mytext").value;
            //document.write("I am here " + mtext);
            ErrorText= "";
            if (mtext == "" ){
                alert("No text !");
                return false;
            }
            if (ErrorText= "") { form.submit() }
    }
    
    checkbox();
    droplist();
    namecheck();
    textcheck();
        }

  • #2
    New Coder
    Join Date
    Feb 2011
    Posts
    36
    Thanks
    18
    Thanked 0 Times in 0 Posts
    My nephew found the problem... a line break between the <textarea> and the </textarea> tags.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Yes, but doesn't it strike you that all a person has to do to pass that so-called validation is enter a single space? Or, as you noted, a line break (Enter)? Is that really good enough for you to accept it?

    Same thing with namecheck(). Is there really any point in having validation if it is that weak?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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