Help - trying to check a form field has content

Discussion in 'JavaScript' started by Jon109, May 1, 2008.

  1. #1
    Hi,

    Before I start, I should say I'm pretty new to Javascript so bear with me.

    My problem is this: I would like to implement a form on our website that will only submit if a particular field is empty. So i am trying to use javascript to check that the field is in fact empty and if it isn't to not submit. I plan to use CSS to hide the field.

    We have had a problem with spambots going thorugh our site and auto completing forms, so this my efforts to stop them. We may move onto CAPTCHA, but we're going to try this first.

    My script so far is:

    re = /^\w+/

    function submitIt(webEnquiry) {
    if (re.test(webEnquiry.words.value)) {
    alert ("Thank you for your enquiry.")
    webEnquiry.words.focus()
    webEnquiry.words.select()
    return false
    }
    if (webEnquiry.words.value == null) {
    return true
    }
    }

    -----------

    <form name="webEnquiry" onSubmit="email.email = true; words.optional='true'; return (validateForm2(this) && submitIt(webEnquiry))" method="post" action="antiSpam-act.cfm">

    ----------

    I think I am almost there, but whenever I seem to amend the regular expression to look for other characters my form fails.

    Like I said at the start I am new to Javascript so don't laugh or be too offended by what I've put together so far.

    I would appreciate any help.

    Thanks.
     
    Jon109, May 1, 2008 IP
  2. progfrog

    progfrog Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i'm new to js as well but still i recommend u'll try
    if (webEnquiry.words.value == "") {
    return true

    hope i've helped.
    goodbye.
     
    progfrog, May 1, 2008 IP
  3. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #3
    My suggestion: you don't need to check it by JavaScript. Since you've already used CAPTCHA, let the server script checks it :)
     
    xrvel, May 2, 2008 IP
  4. Jon109

    Jon109 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Progfrog,

    Thank you, that was a great help. I was overcomplicating it!

    Cheers
     
    Jon109, May 2, 2008 IP