Results 1 to 12 of 12

Thread: [RESOLVED] Help with user defined object - My first attempt!

  1. #1
    Join Date
    Sep 2012

    resolved [RESOLVED] Help with user defined object - My first attempt!

    I'm relatively new to JS so I need a hand. If someone could go to This Page and tell me what I'm doing wrong? I'm basically trying to make an object that checks for a valid name, id number and email address. Since I don't know that much about objects I've become stuck. Any ideas would be greatly appreciated.

  2. #2
    Join Date
    Aug 2008
    I can see several problems here.. I'll try to go through them all.

    First in the validateForm function...

    if (validate.checkBlank = "true")

    should be
    if (validate.checkBlank() == true)
    First, validate.checkBlank is a function/method that checks if any field is blank, right? Then you have to call it (using the parenthesis) to get any value back from it. Second, a single equal sign is used for assigning values to variables and object members, and two equal signs is for testing equality (and here you want to see if checkBlank returns true). Third, remember that the string "true" is not the same as the boolean value true.

    What actually happens right now on that line is that you assign the string "true" to validate.checkBlank (so validate.checkBlank is now a string instead of a function).

    The same problem occurs on the row with if (validate.checkEmail = "true").

    The next problem is in the hwkObj constructor function:

    var formIsBlank = true;
    should probably be
    return true;
    since you at one point in validateForm calls checkBlank to check if any field is blank, and then use the returned value to decide what to do (with an if statement).

    The next problem is this line:
    if (isNaN(id.value) = true)
    which should be
    if (isNaN(id) == true)
    Remember that you put the value of document.form1.fieldId.value in id before, so id now contains a string. Also, we should be using the == here since we're comparing the returned value from isNaN(id) and true, not assigning anything.

    The last problem I can see at the moment is the way you use the regular expression to check the e-mail address. I suggest you check out http://www.w3schools.com/jsref/jsref_regexp_test.asp to see how to use regexp. And also from this function you should be returning a value instead of setting the local variable formBadEmail to true.

    I hope any of this makes any sense and that it helps you. ^^

    Just as an additional note... you usually don't have to compare a value with true in an if statement. E.g. these two if statements both works the same way:
    if (isNaN(id) == true)
    if (isNaN(id))
    Last edited by ReFreezed; 10-23-2012 at 05:41 PM.

  3. #3
    Join Date
    Sep 2012
    Thank you very much. This has definitely put me in the right direction. I appreciate the help. I put in the corrections but I think I'm still missing something. The code still isn't functioning right.
    Last edited by skinman; 10-23-2012 at 06:42 PM.

  4. #4
    Join Date
    Sep 2012
    Quote Originally Posted by skinman View Post
    The code still isn't functioning right.
    What I mean by that is the error message that pops up when the form is blank keeps coming up even when the form isn't blank.

  5. #5
    Join Date
    Aug 2008
    I see two problems remaining (assuming you updated that code in the link you provided):
    if (validate.checkBlank() == null)

    should be
    if (validate.checkBlank() == true)
    Remember that you're only returning true if any field is empty. Also:

    if (email != emailRegExp)

    should be
    if (!emailRegExp.test(email))

    emailRegExp is not a string you compare against, it's an unique RegExp object (so email != emailRegExp will always be true because they are two completely different kind of objects, thus never the same). Check the link I posted to W3schools in my other post to see how RegExp works.

  6. #6
    Join Date
    Sep 2012
    Thanks again for the help. Regular Expressions have always been one of those things that I struggle with due to its complexity. I have been to w3schools site to check it out. It's just so complex that I have issues getting even the simplest implementation down. This email checker isn't helping me any in that matter. In any case I've changed what you mentioned. Unfortunately I can't seem to get the error messages to turn on and off as I want them to. I'm not sure if the problem is the object code or the validation code.

    EDIT: I got everything to work except the email checker. It won't recognize a valid email, so I think my regexp is wrong. If that is what you were trying to tell me before I'm looking into it now.
    Last edited by skinman; 10-25-2012 at 04:11 AM. Reason: Update

  7. #7
    Join Date
    Sep 2012
    I just used a new regex and now the whole script finally works as intended, with one issue. The success message I wanted to use doesn't fire. I think that's because the form is trying to get submitted, but I never put in any code to submit anything. It should be doing nothing but staying on the page. Any suggestions on that one? I've looked at the same code for so long I'm going snow blind, or have tunnel vision and can't see beyond a certain point anymore.

  8. #8
    Join Date
    Aug 2008
    There are problems with this line:
    if (validate.checkBlank == false && validate.checkNumber == false && validate.checkEmail == false)
    Just like before, validate.checkBlank is a function, so you have to call it to get any value back... Also note that you should compare the returned value with true, like this:

    if (validate.checkBlank() != true && validate.checkNumber() != true && validate.checkEmail() != true)
    I don't think I've mentioned it yet, but if a function doesn't return anything specific then it automatically returns undefined. That means your validation functions could potentially return two different values - either true or undefined (since you don't return anything if the if-statement fails.) Thus validate.checkBlank()==false would always fail because checkBlank never return the value false. (false is not the same as undefined.)

    And finally, that whole if-statement should be inside the validateForm() function, shouldn't it? It's outside right now. :)

  9. #9
    Join Date
    Sep 2012
    I kept forgetting the () in there. I also did neglect to insert the closing } of the function. The thing that still bothers me is that the form is still trying to submit itself. Doesn't return false; stop that from happening?

    EDIT: Never mind. I had a typo and forgot the ;. I swear these typos and forgetfulness is going to be the death of me. Anyway, thank you so much for your patience and help with all of this. Now that I have a working code I can study it and learn from it, and I have certainly learned a ton from you.
    Last edited by skinman; 10-25-2012 at 04:48 PM.

  10. #10
    Join Date
    Aug 2008
    That's great! And I'm just glad I could help. ^^

  11. #11
    Join Date
    Sep 2012
    I hate to beat a dead horse here, but do you know how I would make this code so that when a single form field is empty the field border changes and the error message pops up? Right now even if all fields are empty only the first one is highlighted in red.

  12. #12
    Join Date
    Sep 2012
    Never mind I figured it out. I'll let this dead horse rest now.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12671 seconds
  • Memory Usage 2,977KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (13)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (12)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (12)postbit
  • (12)postbit_onlinestatus
  • (12)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates