Jump to content

Another Issue with Javascript


divinedesigns1

Recommended Posts

ok, so now im trying to add a check once the field of the form is filled out correctly, but im having problems outputing the "checked"

 function check(){            var fnlname, addy, numy, email, city, state, country, zip, namecheck;            fnlname = document.getElementById('fnlname');            addy = document.getElementById('addy');            numy = document.getElementById('numy');            email = document.getElementById('email');            city = document.getElementById('city');            state = document.getElementById('state');            country = document.getElementById('country');            zip = document.getElementById('zip');            /*** check to see if 2 sets of words is placed ****/            namecheck = /^[A-Z,a-z][A-Z,a-z]+$/;            if(fnlname.value == ''){                fulname.innerHTML = '<b>full name is missing</b>';                return false;            }else{                if(!namecheck.test(fnlname)){                  fulname.innerHTML = '<b>Both Names Required</b>';                  return false;                  }                            }            /*** if both names are filled in, display a "checked mark" ***/            if(fnlname == namecheck.test(fnlname)){                fulname.innerHTML = '<b>checked</b>';                  return false;             }            if(addy.value == ''){                address.innerHTML = '<b>Address is missing</b>';                return false;            }else{                namecheck = /^[A-Z,a-z]+$/;                if(!namecheck.test(addy)){                  document.getElementById('address').innerHTML = '</b>A full Address is Require</b>';                    return false;                }            }            return true;        }

any tips or hint or help will do just fine

Link to comment
Share on other sites

For namecheck, try this instead: /[A-Z,a-z]s/;

 

test() returns true or false. Have for the if: if(namecheck.test(fnlname.value)) instead of if(fnlname == namecheck.test(fnlname))

Edited by Don E
Link to comment
Share on other sites

For namecheck, try this instead: /[A-Z,a-z]s/;

 

test() returns true or false. Have for the if: if(namecheck.test(fnlname.value)) instead of if(fnlname == namecheck.test(fnlname))

nope, that didnt do it, im trying to make sure both first and last name is filled out

 

Where's 'fulname.' coming from? I don't see any document.getElementById("fulname") from last topic.

that is a span with the id of fulname

Link to comment
Share on other sites

SO its global scope variable outside the function and placed after span with id, or using window.onload? yes!

not too sure what you just say, but im not using a window.onload, so with the 'fulname' and innerhtml, im able to output the correct errors for that input

Link to comment
Share on other sites

You will have a problem with how you are using return, Because when it reaches a invalid condition and you invoke return false everything stops at that point and none of the other if conditional validations are processed, you need to as i suggested in first topic to set a varible valid to true at beginning, then at EACH if condition validation where it fails set valid to false, then AT END return valid varible value.

Edited by dsonesuk
Link to comment
Share on other sites

You will have a problem with how you are using return, Because when it reaches a invalid condition and you invoke return false everything stops at that point and none of the other if conditional validations are processed, you need to as i suggested in first topic to set a varible valid to true at beginning, then at EACH if condition validation where it fails set valid to false, then AT END return valid varible value.

that part works

Edited by DDs1
Link to comment
Share on other sites

ok so i got that working with the valid variable but the statement that is true still aint working

 if(namecheck.test(fnlname.value)){                fulname.innerHTML = '<b>check</b>';                valid = true;               }

this suppose to make sure both first and last name is being input and once this is done a check is suppose to be displayed

Link to comment
Share on other sites

Try and change your pattern namecheck to the following:

namecheck = /[A-Z,a-z]+s[A-Z,a-z]+/;

That looks for letters, a space, and letters only. For i.e.: John Smith, like how names are. If anything that is entered that is not a match to that, it returns false.

if(fnlname.value == ''){    fulname.innerHTML = '<b>full name is missing</b>';    return false;}else{   if(!namecheck.test(fnlname.value)) //if anything doesn't match the pattern above, test() returns false. So in other words, if it is TRUE that test() returns false, enter the if    {      fulname.innerHTML = '<b>Both Names Required</b>';      return false;     }}

If the user entered a pattern match for namecheck/test(), when the script reaches this point:

if(namecheck.test(fnlname)){  fulname.innerHTML = '<b>checked</b>';   return false; }

... since test returns true, we should enter this if and set the innerHTML of fulname to <b>checked</b>.

 

Hopefully this was of some help.

Edited by Don E
Link to comment
Share on other sites

Try and change your pattern namecheck to the following:

namecheck = /[A-Z,a-z]+s[A-Z,a-z]+/;
That looks for letters, a space, and letters only. For i.e.: John Smith, like how names are. If anything that is entered that is not a match to that, it returns false.
if(fnlname.value == ''){    fulname.innerHTML = '<b>full name is missing</b>';    return false;}else{   if(!namecheck.test(fnlname.value)) //if anything doesn't match the pattern above, test() returns false. So in other words, if it is TRUE that test() returns false, enter the if    {      fulname.innerHTML = '<b>Both Names Required</b>';      return false;     }}
If the user entered a pattern match for namecheck/test(), when the script reaches this point:
if(namecheck.test(fnlname)){  fulname.innerHTML = '<b>checked</b>';   return false; }
... since test returns true, we should enter this if and set the innerHTML of fulname to <b>checked</b>. Hopefully this was of some help.

 

it was the plus smh /[A-Z,a-z]+s[A-Z,a-z]+/ i should really just stick to this forum and not listen to my professor lol

once again thank you i greatly appreciate it

Link to comment
Share on other sites

it worked, but question, why did it keep flashing when i have it to valid = true?

Link to comment
Share on other sites

If conditional validation should only change valid to false if it fails validation, otherwise it may have failed validation in previous if condition, but you have reset to true, meaning previous invalid values will always pass validation check.

oh alrite, talk about confusion lol thats why i like my php, but gotta learn javascript

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...