Jump to content
divinedesigns1

Another Issue with Javascript

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...