views:

102

answers:

1
<form action="" method="post">
<div align="center"><legend>Add a Code</legend>
<label for="code"></label>
<input type="text" name="code" id="code" maxlength="10" />
<input type='button' 
 onclick=
             "isAlphanumeric(document.getElementById('code'),'Your Submission Contained Invalid Characters'); 
              isBadPhrase(document.getElementById('code'), 'Please Enter A Correct Friend Code!');"     
     value='Check Field' />

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
 return true;
}else{
 alert(helperMsg);
 elem.focus(); 
 return false;
}

}

function isBadPhrase(elem,helperMsg){
var badPhrase=/EPW|ESW|\s/;
if (elem.value.match(badPhrase)){
 alert(helperMsg);
 elem.focus();
 return false;
}else{
 return true;
 }

}

What is wrong here?

A: 

You aren't returning anything from your handler. You need to return a value (true/false) from the handler, especially if you want to stop the default. Note -- I'd also change the name of the "BadPhrase" method to "GoodPhrase" so that it matches the sense of the return value.

script:

function isAlphanumeric(elem, helperMsg){
    var alphaExp = /^[0-9a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
        return true;
    } else {
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

function isGoodPhrase(elem,helperMsg){
    var badPhrase=/EPW|ESW|\s/;
    if (elem.value.match(badPhrase)){
        alert(helperMsg);
        elem.focus();
        return false;
    } else {
        return true;
    }
}

function checkInput(id)    {
    return isAlphanumeric(document.getElementById(id),'Your Submission Contained Invalid Characters')
        && isGoodPhrase(document.getElementById(id), 'Please Enter A Correct Friend Code!');
}

html:

<input type='button' onclick="return checkInput('code');" value='Check Field' />

EDIT:

If you do the validation on form submit it will run regardless of how the form is submitted. You can add it as a onsubmit handler in the form tag, like you did for the click handler of the button. The button should probably be a submit button if it is supposed to trigger form submission.

Note: my preference would be to add the handlers via javascript rather than in the tag, but that's outside the scope of the question. I'd also probably use a framework rather than raw javascript, but that's also outside the scope.

tvanfosson
What do you mean? Can you be more specific?
It worked until I added the badPhrase part...?
Is that correct? It doesn't work. I think you are missing something. When I plug it into my editor all my codes colors are changed and I assume that means broken,.
nvm you added a "
I had an extra double-quote at the end. I've fixed it. Note that I changed the name of the function, too.
tvanfosson
Still doesn't work. It allows !, spaces, and EPW, ESW.
Did you change the name of your function (or change my snippet back)? Sounds like maybe you have a javascript error on the page that is preventing the code from executing at all.
tvanfosson
Try running it in Firefox/Firebug and see what the console tells you.
tvanfosson
I added all the parts of your code, left my original functions just so i didn't lost them.
Nothing, just some CSS errors.
Did you try putting in some breakpoints in the script and examining the values during execution?
tvanfosson
I just implemented this in a local file and it worked fine in Firefox. I pasted the entire code that I used in my answer.
tvanfosson
Okay, so I apologize, tvanfosson. Your code does work. I am incredibly stupid. I am the type something and press return to input data, not actually click it. Please accept my apology. Now, I need to figure out how to make it so if people press 'return' instead of pressing the actual button for it to return an error.
Run the validation code on the form submit event rather than on button click. That way it will run regardless of how the form is submitted.
tvanfosson