Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-30-2012, 06:12 AM   PM User | #1
jc@rcc
New Coder

 
Join Date: Jun 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jc@rcc is an unknown quantity at this point
Question how do I check that a text field only contains the letter x

I am trying to get javascript to submit a form only if the user has entered a capital X in a text box (named "tictactoe3"). I only want the form to submit if the character enter is an X. I do not want it to submit if any other character is entered or if more than one character is entered or if the field is left blank.
jc@rcc is offline   Reply With Quote
Old 06-30-2012, 07:39 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jc@rcc View Post
I am trying to get javascript to submit a form only if the user has entered a capital X in a text box (named "tictactoe3"). I only want the form to submit if the character enter is an X. I do not want it to submit if any other character is entered or if more than one character is entered or if the field is left blank.
Code:
<form>
<input type = "text" name = "tictactoe3" size = "1" maxlength = "1" >
<input type = "button" value = "Submit the form" onclick = "check4X()">
</form>

<script type = "text/javascript">
function check4X() {
var val = document.forms[0].tictactoe3.value;
if (val === "X") {
document.forms[0].submit();
}
else {
alert ("You must place an X in the box before you can submit the form");
document.forms[0].tictactoe3.value = "";
return false;
}

}
</script>
Celebrity: 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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
jc@rcc (06-30-2012)
Old 06-30-2012, 08:32 AM   PM User | #3
jc@rcc
New Coder

 
Join Date: Jun 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jc@rcc is an unknown quantity at this point
Thanks for the reply.

I should have known the maxlength = "1" and that stops them being able to enter more than one character

But the javascript doesnt work. All it does is return the alert whether the text field is empty or if anything is entered.

And why the three =
Quote:
Originally Posted by Philip M View Post
if (val === "X")
I've tried it with two = but it still does thje same.
jc@rcc is offline   Reply With Quote
Old 06-30-2012, 09:00 AM   PM User | #4
jc@rcc
New Coder

 
Join Date: Jun 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jc@rcc is an unknown quantity at this point
Here is my javascript so far, and it all works:-
Code:
function validateForm()
{
	var x=document.forms["Contact"]["leavethisblank"].value
	var y=document.forms["Contact"]["hummie"].checked
	var t1=document.forms["Contact"]["tictactoe1"].value
	var t2=document.forms["Contact"]["tictactoe2"].value
	var t4=document.forms["Contact"]["tictactoe4"].value
	var t5=document.forms["Contact"]["tictactoe5"].value
	if (x==0 && y == false && t1==0 && t2==0 && t4==0 && t5==0)
   	return true
	else
	return false	
}
"leavethisblank" is a hidden field and the form only submits if it is left empty.
"hummie" is a checkbox and the form only submits if it is unchecked.
"tictactoe1,2,4 &5" are fields in a tictactoe game and the form only submits if they are left empty.
"tictactoe3" is another field in the tictactoe game that I want users to put an X in order to make a line of three. I only want the form to submit if an X is inserted in this field.
jc@rcc is offline   Reply With Quote
Old 06-30-2012, 09:00 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jc@rcc View Post
Thanks for the reply.

I should have known the maxlength = "1" and that stops them being able to enter more than one character

But the javascript doesnt work. All it does is return the alert whether the text field is empty or if anything is entered.

And why the three =

I've tried it with two = but it still does thje same.
Sorry, the script works just fine for me. The alert is shown if anything except a capital letter X (or nothing at all) is entered into the box. That is what you asked for. If a capital X is entered the form is submitted (to what server-side script???)

=== is stricty equality - value and type. == will work fine here as well. If the required value was a number then 1==1 but 1 != "1".

You say "leavethisblank" is a hidden field and the form only submits if it is left empty. But you are testing for a value of 0. Same with other fields. So it cannot possibly work.
BTW, 0 is not the same as O.

Your code is sloppy - you have left out the semi-colons at the end of each line and have not used braces around your if/else.
__________________

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.

Last edited by Philip M; 06-30-2012 at 09:11 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
jc@rcc (06-30-2012)
Old 06-30-2012, 10:31 AM   PM User | #6
jc@rcc
New Coder

 
Join Date: Jun 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jc@rcc is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Sorry, the script works just fine for me.
I've tried it on its own in the html and on its own in a seperate javascript file linked to the html. I'll try it on a form with only the one field to see if its anything to do with the rest of my form.

Quote:
Originally Posted by Philip M View Post
You say "leavethisblank" is a hidden field and the form only submits if it is left empty. But you are testing for a value of 0. Same with other fields. So it cannot possibly work.
BTW, 0 is not the same as O.
It works for me! If I put anything in these fields the form does not submit.

Quote:
Originally Posted by Philip M View Post
to what server-side script???
At the moment the page is not live (it is part of a complete site revamp I am working on). At the moment when the form submits it redirects to a Thank You page and sends an email via my existing sites server. I need to work on the server side validation of the form, but haven't got a clue how to do this yet!

Quote:
Originally Posted by Philip M View Post
Your code is sloppy - you have left out the semi-colons at the end of each line and have not used braces around your if/else.
I'll tidy it up a bit!
jc@rcc is offline   Reply With Quote
Old 06-30-2012, 10:47 AM   PM User | #7
jc@rcc
New Coder

 
Join Date: Jun 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jc@rcc is an unknown quantity at this point
OK. So I've put it all into a new html page and it works!!! Wonder what I'm doing wrong when I try to put it into my form then?
jc@rcc is offline   Reply With Quote
Old 06-30-2012, 10:51 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jc@rcc View Post
OK. So I've put it all into a new html page and it works!!! Wonder what I'm doing wrong when I try to put it into my form then?
Who can tell?

"It works for me! If I put anything in these fields the form does not submit." No. It does not submit because of another error which I have pointed out.
__________________

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.

Last edited by Philip M; 06-30-2012 at 10:53 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
jc@rcc (06-30-2012)
Old 06-30-2012, 11:25 AM   PM User | #9
jc@rcc
New Coder

 
Join Date: Jun 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jc@rcc is an unknown quantity at this point
Thumbs up

TA DAH!!! It works!!

Code:
function validateForm()
{
	var x=document.forms["Contact"]["leavethisblank"].value;
	var y=document.forms["Contact"]["hummie"].checked;
	var t1=document.forms["Contact"]["tictactoe1"].value;
	var t2=document.forms["Contact"]["tictactoe2"].value;
	var t3=document.forms["Contact"]["tictactoe3"].value;
	var t4=document.forms["Contact"]["tictactoe4"].value;
	var t5=document.forms["Contact"]["tictactoe5"].value;
	if (x==0 && y == false && t1==0 && t2==="x" && t3==0 && t4==0 && t5==0){
   	return true;}
else {
return false;}
}
I've moved things about a bit and its now "tictactoe2" that needs an "x" in it.

The problem had something to do with onsubmit in the form tag rather than onclick in the submit button tag. It also had something to do with trying to replace the letter entered with a capital - have changed it to remain lower case and to look for a lower case "x".

Thanks. Now all I need to do is learn how to do the servere side bit.

Last edited by jc@rcc; 06-30-2012 at 11:50 AM..
jc@rcc is offline   Reply With Quote
Old 06-30-2012, 11:27 AM   PM User | #10
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Ah I wasn't aware you could use === in JavaScript....

Is it a new feature? :S

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 06-30-2012, 12:08 PM   PM User | #11
jc@rcc
New Coder

 
Join Date: Jun 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jc@rcc is an unknown quantity at this point
And heres how this bit of the form will look when it goes live:

http://www.wellingtont2905.co.uk/spam.html
jc@rcc is offline   Reply With Quote
Old 06-30-2012, 12:55 PM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by LearningCoder View Post
Ah I wasn't aware you could use === in JavaScript....

Is it a new feature? :S

Regards,

LC.
No, not at all a new feature.

For more info see http://stackoverflow.com/questions/3...operator-i-use
__________________

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.

Last edited by Philip M; 06-30-2012 at 12:58 PM..
Philip M is offline   Reply With Quote
Old 06-30-2012, 01:25 PM   PM User | #13
jc@rcc
New Coder

 
Join Date: Jun 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jc@rcc is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
(to what server-side script???)
So any clues how I do the server side script? I am completely new at this. Or any idea where I can learn how to do it?
jc@rcc is offline   Reply With Quote
Old 06-30-2012, 01:33 PM   PM User | #14
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jc@rcc View Post
So any clues how I do the server side script? I am completely new at this. Or any idea where I can learn how to do it?
I think you are under-estimating what is required here. One does not learn a server-side language in a few days or a few weeks.
__________________

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.
Philip M is offline   Reply With Quote
Old 06-30-2012, 02:41 PM   PM User | #15
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
There is no need of PHP, form or to detext X, to play TicTacToe ! Javascript is enough, see this page.
007julien is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:03 PM.


Advertisement
Log in to turn off these ads.