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 to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question JavaScript Textarea Tag Validation

    Hey, i am working on a "Quick Contact" e-mailer for a website and i would like to use JavaScript To Validate That Something has been entered into the textarea tags, here is my whole script of the test page (for validating NOT emailing i made)...(I do not need to validate the email with javascript i am use php for that and have already gotten it work)[also right now i am just using alert boxes, i should be able to switch it to forms by my self, but if you just want to put it in forms that would be fine, Thanks]
    Code:
    <html>
    	<head>
    		<title>Test1</title>
    		<script>
    			function Submit(){ /*alert('It is running the function');*/
    				var y;
    				y=document.getElementById('YN');
    				if (y =='Test...'){
    					alert('Please Give Your Name');
    					window.location = 'test1.php';
    				}
    				else {
    					if (y ==''){
    					alert('Please Give Your Name');
    					}
    					else {};
    				};
    			};
    		</script>
    	</head>
    	<body>
    		<textarea Name="YN" Id="YN" onfocus="if(this.value == 'Test...'){this.value = '';}" onblur="if(this.value == ''){this.value='Test...';}" rows="1" cols="20">Test...</textarea>
    		<input type="submit" onClick="Submit();" value="TEST" />
    	</body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Code:
    <textarea Name="YN" id="YN" onfocus="if(this.value == 'Test...'){this.value = '';}" onblur="if(this.value == ''){this.value='Test...';}; checkempty(this)" rows="1" cols="20">Test...</textarea>
    
    <script type = "text/javascript">
    function checkempty(which) {
    var val = which.value;
    if (val == "Test...") {
    alert ("You must enter a value in the textarea");
    return false;
    }
    }
    
    </script>
    These days alerts are used only for debugging. Prefer to use DOM methods to display an error message to the user.

    Note that form validation of the pattern if (document.formname.formfield.value == "") - that is blank - 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. A proper name may only contain letters, hyphen, space and apostrophe. 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.

    Politician: It's Catch 22. You need to nip it in the bud before it bites you in the backside.
    Interviewer: Yes, it's cat and mouse, isn't it? - Al-Jazeera English.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    iGeekiHackiMatt (07-01-2012)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank You for the help, I had the blank because if a browser was slow or something and didn't load the onblur...
    thanks again.... I Changed it a little to make it so it runs with the button here the script is, thanks
    Code:
    		<textarea Name="YN" id="YN" onfocus="if(this.value == 'Test...'){this.value = '';}" onblur="if(this.value == ''){this.value='Test...';};" rows="1" cols="20">Test...</textarea>
    		<input type="button" onClick="checkempty(YN)" value="TEST" />
    	</body>
    		<script type = "text/javascript">
    			function checkempty(which) {
    			var val = which.value;
    			if (val == "Test...") {
    				alert ("You must enter a value in the textarea");
    				return false;
    				}
    			}
    		</script>
    Last edited by iGeekiHackiMatt; 07-01-2012 at 06:59 PM.


  •  

    Tags for this Thread

    Posting Permissions

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