...

View Full Version : JavaScript Textarea Tag Validation



iGeekiHackiMatt
07-01-2012, 04:23 PM
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]

<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>

Philip M
07-01-2012, 05:32 PM
<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.

iGeekiHackiMatt
07-01-2012, 05:48 PM
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
<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum