0

This is kind of a second part of an earlier question. Someone suggested I post a new question with a JS Fiddle.

For some reason my JavaScript skips the Phone box, it should be getting:

First Name
Last Name
Email Phone

Here is the Java:

<!-- Begin
<
script type = "text/javascript" > function _validate() 
    var emVal, fnVal, lnVal, phoneVal, addressVal;
    var _fname = document.getElementById('first_name').value;
    var _lname = document.getElementById('last_name').value;
    var _email = document.getElementById('email').value;
    var _phone = document.getElementById('phone').value;
    var _address = document.getElementById('address').value;
    var _lblError = document.getElementById('lblError');




    if (_email != "" || _email != null) {
        var b = _emailValidator(_email);
        if (b == false) {
            _lblError.innerHTML = "Invalid Email";
            emVal = false;
        } else {
            fnVal = true;
            if (_fname == "" || _fname == null) {
                _lblError.innerHTML = "Enter First Name";
                fnVal = false;
            } else {
                lnVal = true;
                if (_lname == "" || _lname == null) {
                    lblError.innerHTML = "Enter Last Name";
                    lnVal = false;
                } else {
                    phoneVal = true;
                    if (_phone == "" || _phone == null) {
                        lblError.innerHTML = "Enter Phone";
                        phoneVal = false;
                    } else {
                        addressVal = true;
                        if (_address == "" || _address == null) {
                            _lblError.innerHTML = "Enter your Address";
                            addressVal = false;
                        } else {
                            addressVal = true;
                            if (emVal == true && fnVal == true && lnVal == true && phoneVal == true && addressVal == true) {
                                postIt();
                            }

                        }
                    }
                }

            }


        }
    } else {
        _lblError.innerHTML = "Enter Email ID";
        emVal = false;
    }


}

function _emailValidator(_email) {
    var a;
    var lastAtPos = _email.lastIndexOf('@');
    var lastDotPos = _email.lastIndexOf('.');
    if (lastAtPos < lastDotPos && lastAtPos > 0 && lastDotPos > 2 && (_email.length - lastDotPos) > 2) {
        a = true;
    } else {
        a = false;
    }
    return a;
}

function hideIt() {
    document.getElementById('lblError').innerHTML = "";
}

function postIt() {
    document.forms["_form"].submit();
}

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;

    return true;
}

< /script>

</head >

//  End -->
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Never do what you did in your last edit. Question and answers are also supposed to help other people. After your edit it is not clear at all what the question was about. Also mark the answer as accepted that helped you the most. – Felix Kling Apr 16 '14 at 15:34
  • I've voted to close as "typo", since the question is rather niche, and it is not likely to be useful to readers. – halfer Dec 28 '21 at 15:44

2 Answers2

2

It's commented out with <!-- and -->.

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

It is because it is all inside a comment.

The code is never executed. You need to remove the <!-- --> tag

You should probably write it like this :

<!-- Begin -->
    <script type="text/javascript">
         //script goes here
    </script>
<!-- End -->
Adjit
  • 10,134
  • 12
  • 53
  • 98
  • 1
    Also, `< script` is **not** the same as ` – Toothbrush Apr 16 '14 at 14:05
  • Hi there, for some reason it's not working on mine? Or maybe I'm doing it wrong. Could you edit it on JSFIDDLE? –  Apr 16 '14 at 14:07
  • Is it just me or it's still skipping the phone box? –  Apr 16 '14 at 14:08
  • 1
    @user3541252 seems to work fine for me. I don't know what you mean by skipping – Adjit Apr 16 '14 at 14:10
  • For example, when submitting it doesn't verify the phone box that it's not a number or that nothings in there. –  Apr 16 '14 at 14:11
  • @user3541252 You are only checking if it's not empty. – Toothbrush Apr 16 '14 at 14:11
  • @toothbrush even if it's empty it skips. I see what he's saying – Adjit Apr 16 '14 at 14:12
  • @toothbrush But shouldn't it display an error like the rest? e.g First Name? –  Apr 16 '14 at 14:13
  • Oh, I see what the problem is... It's not executing the JavaScript. – Toothbrush Apr 16 '14 at 14:15
  • @toothbrush Yeah, I don't know why? –  Apr 16 '14 at 14:15
  • @user3541252 also, you need to fix your email validator. if you put in something like `s@c` when you hit submit there is a post error – Adjit Apr 16 '14 at 14:16
  • @metsales Yeah you are required to put something after @ and .com –  Apr 16 '14 at 14:17
  • @toothbrush It's still not executing the phone box? –  Apr 16 '14 at 14:18
  • @user3541252 right, but try it, and see what happens – Adjit Apr 16 '14 at 14:18
  • @user3541252 Okay, I am not sure exactly why that happens, but I can only suspect that is because you have 5/6 nested if/else statements. – Adjit Apr 16 '14 at 14:28
  • @metsales I've tried literally everything and can't get it to work. –  Apr 16 '14 at 14:29
  • @user3541252 I may have figured this out... there are a lot of discrepancies between your html code and your javascript. For instance, when the user clicks the submit button, it calls a non-existent function. Your submit method... how does the program know what `_form` is? There is no element with the `id` `lblError`. Your form itself has to have a post method if you are going to call `.submit` but there is none. You just need to go through this line by line and actually figure out the code. I can do it all for you, but that does the both of us no good. – Adjit Apr 16 '14 at 14:36
  • @user3541252 See http://jsfiddle.net/fXrPn/3/. Does that do what you want? You can obviously adapt it to your needs. – Toothbrush Apr 16 '14 at 14:37
  • I mean if you could edit it on jSfiddle it would be great, again I'm just using this for my website. –  Apr 16 '14 at 14:37
  • @toothbrush Ah, you're a legend pal. But the thing what worries me if I adapt that to my code it may not work :s –  Apr 16 '14 at 14:39
  • @toothbrush I've used toothbrushes html ONLY html and it fixed my problem. Wow. Thanks a lot guys. But I am unsure of how to make phone number only numbers? Max 10 minnimum 8? Cheers –  Apr 16 '14 at 14:51
  • @user3541252 right here : http://stackoverflow.com/questions/1779013/check-if-string-contains-only-digits as for length just call `.length` on the string – Adjit Apr 16 '14 at 14:54
  • @user3541252 try this one instead : http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric – Adjit Apr 16 '14 at 15:00
  • @metsales The thing is I'm unsure of where to put it? –  Apr 16 '14 at 15:02
  • @user3541252 it's a function. So put it the same way you put the other functions and call it as necessary – Adjit Apr 16 '14 at 15:55