...

View Full Version : how do I check that a text field only contains the letter x



jc@rcc
06-30-2012, 06:12 AM
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.

Philip M
06-30-2012, 07:39 AM
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.


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

jc@rcc
06-30-2012, 08:32 AM
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 =


if (val === "X")
I've tried it with two = but it still does thje same.

jc@rcc
06-30-2012, 09:00 AM
Here is my javascript so far, and it all works:-


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.

Philip M
06-30-2012, 09:00 AM
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.

jc@rcc
06-30-2012, 10:31 AM
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.


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.


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!


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
06-30-2012, 10:47 AM
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?

Philip M
06-30-2012, 10:51 AM
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? :rolleyes:

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

jc@rcc
06-30-2012, 11:25 AM
TA DAH!!! It works!!:thumbsup:



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.

LearningCoder
06-30-2012, 11:27 AM
Ah I wasn't aware you could use === in JavaScript....

Is it a new feature? :S

Regards,

LC.

jc@rcc
06-30-2012, 12:08 PM
And heres how this bit of the form will look when it goes live:

http://www.wellingtont2905.co.uk/spam.html

Philip M
06-30-2012, 12:55 PM
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/359494/javascript-vs-does-it-matter-which-equal-operator-i-use

jc@rcc
06-30-2012, 01:25 PM
(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?

Philip M
06-30-2012, 01:33 PM
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.

007julien
06-30-2012, 02:41 PM
There is no need of PHP, form or to detext X, to play TicTacToe ! Javascript is enough, see this page (http://mamisab.chez-alice.fr/tictactoe.htm).

jc@rcc
06-30-2012, 04:39 PM
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.
I know it isn't that easy, but what languages are we talking about?


There is no need of PHP, form or to detext X, to play TicTacToe ! Javascript is enough, see this page (http://mamisab.chez-alice.fr/tictactoe.htm).
Good game, but it was not about playing tictactoe, it is about creating an anti spambot contact form. There is a hidden field that needs to be left blank, a checkbox that must be unchecked and an "x" that needs to be placed in only one of 5 text fields (the others of which need to be left blank)!!!

Philip M
06-30-2012, 04:49 PM
I know it isn't that easy, but what languages are we talking about?



I would suppose PHP these days. Perhaps PERL.

Note that human spammers can submit your form simply by disabling Javascript.

jc@rcc
06-30-2012, 05:02 PM
OTT? It's only untick a box and put an "x" in an obvious place! I just don't like spam (http://www.youtube.com/watch?v=anwy2MPT5RE)!!:D

When I created the site in 2009 I had a simple contact page with not anti spam meassures, I didn't know I needed them and after a while I was recieving 4 or 5 spam responses a day! Since then I have just had a page with a "contact me at name(at)address.co.uk (replace the (at) with @" message. With the site revamp I wanted to reinstate the contact form and read about these alternative methods to captcha.

Here is the website (http://www.wellingtont2905.co.uk) as it is at the moment.

Additional:

Have just found this tutorial (http://coredogs.com/lesson/form-and-php-validation-one-page). Will take me a while to digest and adapt the php, but it looks do-able.:)

LearningCoder
06-30-2012, 08:37 PM
No, not at all a new feature.

For more info see http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use

Thank you for the link. I have a JavaScript book from Wrox which isn't too old, yet it says nothing about the equality operator in there....

Again, thank you for the link, i'll give that a read!.:thumbsup:

Regards,

LC.

jc@rcc
07-01-2012, 09:10 AM
Note that human spammers can submit your form simply by disabling Javascript.

That's why I want to do the server side as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum