View Full Version : Validation
zoobie
09-08-2002, 12:26 AM
This used to work. I have the clear onfocus hooked up to the textarea and have "Type Message Here" centered by 10 spaces.
<SCRIPT>
<!--
function checkData() {
var correct = true
if (document.mail.comments.value == "") {correct = false; alert("Please type your message.")}
if (document.mail.comments.value == "space;space;space;space;space;space;space;space;space;space;Type Message Here") {correct = false; alert("Please type your message.")}
return correct
}
//-->
</SCRIPT>
Fix?
Thanks :p
ConfusedOfLife
09-08-2002, 12:59 AM
Are you simply trying to validate a textarea?! First of all, those "space;"s that you wrote, were not declared anywhere, and if I'm not wrong ( I mean it's what you want ), I think it's better that you write this:
<script>
message = " Please Type your message in here";
function Validate()
{
if ( document.forms[0].oArea.value == "" ||
document.forms[0].oArea.value == message )
return false;
return true;
}
</script>
<form onsubmit="return Validate()">
<textarea name="oArea" value="" onclick="this.value=''" cols=10></textarea>
<input type="submit" value="submit">
</form>
<script>
document.forms[0].oArea.value = message;
</script>
I didn't test it, but it's supposed to work!
zoobie
09-08-2002, 02:17 AM
Yes...I'm "simply" trying to validate a textarea. However, it must be validated 2 ways. I manually centered the value of the textarea with "Type Message Here". Since I can't write n b s p; here, I used the word 'space'. This gets cleared with onfocus.
Firstly, it needs to be validated to see if the textarea has been cleared via the onfocus script. It must see if "space;space;space;space;space;space;space;space;space;space;Type Message Here" is, indeed there. If it is, alert.
Secondly, it needs to check if the textarea is then blank (cleared)which I have working fine.
Like I said...this worked last year...
Thanks :p
zoobie
09-08-2002, 03:36 AM
Well, I don't want to bloat my code over this simple validation by adding 2 scripts...lol.
Gotta be an easier way. Why isn't my initial code working?
Btw.. &nbsp; really doesn't display unless you trick it. :D
I'd just do this:
<script language="javascript" type="text/javascript">
function checkData(form) {
if (form.comments.value == "") {
alert("Empty blah");
form.comments.focus();
return false;
}
if (form.comments.value.indexOf("Type Message Here")!= -1) {
alert("No Change blah.");
form.comments.select();
form.comments.focus();
return false;
}
return true;
}
</script>
...then in the body...
<form name="mail" action="javascript: alert('Submission Successful')" method="post" onsubmit="return checkData(this)">
<textarea name="comments" cols="55" rows="5" >
Type Message Here
</textarea>
<input type="submit" value="Send">
</form>
You don't need to declare a boolean value (i.e. correct) because you can use the return statement itself.
IMO, I wouldn't worry about declaring the non-breaking spaces in the script.
joh6nn
09-08-2002, 06:26 AM
you didn't mention what the problem with the original script is? does it run, but not work, or does it throw up an error somewhere?
zoobie
09-08-2002, 07:10 AM
Thanks...I'm working on it. This is the thing..I've centered the "Type Message Here" in the textarea's value by adding spaces. This throws off all validation because it has spaces in front of it to center it. :p My onfocus/clear script then clears the textarea.
The previous validation worked fine last year by using if (document.mail.comments.value == "(10 spaces here to center)Type Message Here") {correct = false; alert("Please type your message.")} like in my first post above.
Now, it doesn't work...No errors or anything...It just doesn't validate.
Perhaps someone could tell me why it doesn't work now as I'd like to learn from this if possible. Perhaps the onfocus/clear script is conflicting.
Thanks :rolleyes:
joh6nn
09-08-2002, 07:32 AM
well, to me, that code looks fine, so i'd say to understand why it isn't working, we have to see the rest of the code.
Hi,
It's interesting. It won't event begin to work for me unless i use the indexOf() method.
function checkData() {
var correct = true
if (document.mail.comments.value == "") {
alert("Please type your message 1.")
correct = false;
}
if (document.mail.comments.value.indexOf("Type Message Here")!= -1){
alert("Please type your message 2.")
//debug alert
alert(document.mail.comments.value)
correct = false;
}
return correct
}
Other things that popped up:
The nbsp; is being interpreted as data. There is a leading space in front of the centered text, which is also being interpreted as data. The form will submit if the textarea is blank and if either the leading space is still there or the textarea contains nbsp;
Taking the nbsp; out of the indexOf('string') string helped it work better and it didn't matter if the nbsp; was in the textarea (aside from the probs. above).
whammy
09-08-2002, 03:30 PM
Have you considered using defaultValue ?
if(this.value == this.defaultValue){alert('Booya!')}
zoobie
09-08-2002, 09:12 PM
Whamster got it...Thanks, man. I knew there had to be a simple way of doing it.
Anyone want to take a shot at my other (http://www.codingforums.com/showthread.php?s=&threadid=5513) validation problem? :D
whammy
09-09-2002, 02:48 AM
You're welcome. :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.