0

This is my first time trying javascript and having trouble getting it to work. I have a php file with a text box and a submit button. Upon clicking the submit button, I want javascript to preform a validation to make sure the text box is not empty. Please see my code below. Upon running, nothing happens when I click the submit button.

<html><head><script type="text/javascript">
function formValidator() {        
    var client_name = document.getElementByID('client_name');        
    if(notEmpty(client_name, "Client Name Blank")){
          return True;       
    }
    return False;         
 }   

function notEmpty(elem, helperMSG) {
    if(elem.length == 0) {
        alert(helperMSG);
        elem.focus();
        return False;
        }
     return True;   
    }
</script></head>

<?php
    echo '<body><form onsumbit="return formValidator()" method="post">
         <table border="1" cellspacing="0" cellpadding="0"><tr><td colspan="2">
         <h1>Add Account</h1></td></tr>
         <tr>
         <td>Client Name: <font size="1">(required)</font></td>
         <td><input type="text" id="client_name" name="client_name" size="35"/></td></tr>
         </table>
         <input type="submit" name="submit_account" value="Add Account"/>
         </form></body></html>';
?>
6
  • Shouldn't you be closing the <html> after the ?> Commented May 21, 2013 at 16:44
  • @harsha The <body> tag is opened and closed in the echo statement (<html> closing tag is there too), so it will end up being valid HTML when sent to the client as far as I can tell. Why it has to be echoed instead of just being plain markup, I have no idea. Commented May 21, 2013 at 16:47
  • @ajp15243 : sorry , I meant </html> Commented May 21, 2013 at 16:48
  • 1
    @harsha As long as the final markup after PHP processing is valid markup, it is fine. This looks like it will be valid, as far as closing <html> and such goes. Commented May 21, 2013 at 16:49
  • replace: True->true and False->false Commented May 21, 2013 at 16:52

3 Answers 3

1
  1. JavaScript is case sensitive. True and False are not defined (you mean true and false) so, on encountering them, the JavaScript engine will throw an exception and normal form submission will resume.
  2. You are testing the length property of an HTMLInputElement which (unless you define it otherwise) will always be undefined. You want to test the length of the value. elem.value.length.
Sign up to request clarification or add additional context in comments.

Comments

0

Few issues:

1) True and False, are not recognized as JavaScript is case sensitive. Try using true and false.

2) You also need to use the elem.value.length (or just the elem.value)

so change

elem.length

to elem.value.length.

3) No need for two check of true / false, one is enough.

html><head><script type="text/javascript">
function formValidator() {        
    var client_name = document.getElementByID('client_name');        
    return notEmpty(client_name, "Client Name Blank"); // no need for true false
 }   

function notEmpty(elem, helperMSG) {
    if(elem.value.length == 0) { ///changed to use value.
        alert(helperMSG);
        elem.focus();
        return false;  //change to lowercase false
    }
     return true;   //change to lowercase true.
  }
</script></head>

4 Comments

I think that -1 was on the very first draft of your answer.
I did not, I just noticed it there shortly after you first posted your answer.
np. tx..it's rather annoying, to -1 with no comment, I think they should make it a must to comment to -1, or at least give you like 1 minute after you post before you can -1...
Dory, I tried your code but I am still not getting an alert message saying "Client Name Blank" when I leave the text box blank and click the submit button.
0

Use this:-

 <script type="text/javascript">
    function formValidator() {        
        var client_name = document.getElementByID('client_name');        
        if(notEmpty(client_name, "Client Name Blank")){
              return true;        // Changed True to true
        }
        return false;         // Changed False to false
     }   

    function notEmpty(elem, helperMSG) {
        if(elem.value.length == 0)    // checking length of an element's value
 { 
            alert(helperMSG);
            elem.focus();
            return false;   // Changed False to false
            }
         return true;  // Changed True to true
        }
    </script>

6 Comments

You should explain what you changed and why the change was needed, rather than just posting code and making others hunt for what changes you made.
I think the -1 was given back when you had no explanation, too, and users aren't informed if someone edited an answer on which they voted, so they have to come back and look themselves to decide if the -1 should be removed.
or it could also be someone doing a stratigic -1..people do that too :(
@dory oops, mixed jquery and javascript. Anyways, i have corrected it.
np.can you at least then give me 1 up on my answer ? :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.