-1

i'm trying to put together some Javascript form validation to check that all fields have been completed before submitting.

So far i have this in my tag, and the function for the first form field only:

<script type="text/javascript">

function validate(form) {
fail = vaildatecompany_name(preview.company_name.value)

if (fail =="" return true

else { alert(fail); return false }
}

function validatecompany_name(field) {
    if (field == "" return "No Company Name was entered. \n"
return ""
}
</script>

And the first field i'm trying to validate is:

<form action="jobpreview.php" name = "preview" method="post" form enctype="multipart/form-data" onSubmit = "return validate(this)">

<ul>
<li><label for = "company_name">Company Name</label>    
<input type="text" name="company_name"/>
</li>

Can someone explain what i'm doing wrong? It's not bringing up any alert when i submit the form with no entry in the field and is just going through as normal.

Thanks Dan

1

4 Answers 4

1

Your code is riddled with errors. You spelled your call to the validatecompany_name function wrong, your if statements aren't properly formed (errant opening bracket) and you don't pass the form variable properly (you treat preview and form interchangeably). This works, although it's not great code:

function validate(form){
  var fail = validatecompany_name(form.company_name.value);
    if (fail==""){
      return true;
    } else {
      alert(fail);
      return false;
    }
  }

function validatecompany_name(field){
  if (field == "") return "No Company Name was entered. \n";
  return "";
}
Sign up to request clarification or add additional context in comments.

7 Comments

BTW you don't need the \n bit in your return from validatecompany_name, it doesn't achieve anything.
thanks Ben, this is actually copied from an O'Reilly book (but the typos on my behalf are terrible). Thanks for your time, this is working now. Thanks, Dan
can we at least do return (field === "")?"No Company Name was entered":"";
@Ben Poole Hi Ben, I spoke too soon, sort of. If i go on to add the same validation for the other fields in my form, it only seems to check the last one?
@Dan well you're using the same fail flag for everything I guess, so it's only as good as your last test. Like I say, this isn't great code, I'd be inclined to use something like the jquery.validate plugin myself -- a lot more powerful, and once you've got the hang of it, you'll use it again and again.
|
1

You need closing parentheses on your if statements if (BOOLEAN)

1 Comment

Well that's a little embarrassing! OK, i've put the closing parentheses in but it's still not recognising the empty field on submit. Any ideas?
0

wat is this statement do proper closings... if (field == "" return "No Company Name was entered. \n" it should be like if (field == "") return "No Company Name was entered. \n"

and you cannot directly access form by name.. use document.formname or use the form object that you passed...

1 Comment

check the value of preview.company_name.value in alert... and comment all other codes... also try ` if (field.trim() == "") `
0

Fixing your script since you had a few typos (validatecompany_name misspelled, assignment instead of comparison, missing )/}), which stopped your alerts from firing. Here is a a minimal working version just fixes those mistakes:

function validateCompanyName(field) {
  return field === '' ? 'No Company Name was entered.\n' : '';
}

function validate(form) {
  const fail = validateCompanyName(form.company_name.value);
  if (!fail) return true;
  alert(fail);
  return false;
}
<form action="jobpreview.php" method="post" enctype="multipart/form-data" onsubmit="return validate(this)">
  <ul>
    <li>
      <label for="company_name">Company Name</label>
      <input type="text" name="company_name" id="company_name">
    </li>
    <li>
      <button type="submit">Submit</button>
    </li>
  </ul>
</form>

Improving your script

  1. remove all script and add required
    <input type="text" name="company_name" id="company_name" required> OR

  2. Use recommended event listeners

window.addEventListener('load', () => {
  document.getElementById('jobForm').addEventListener('submit', e => {
    const companyName = e.target.company_name;
    if (companyName.value.trim() ==="") {
      alert('No Company Name was entered.');
      companyName.focus();
      e.preventDefault();
    }
  });
});
<form action="jobpreview.php" id="jobForm" method="post" enctype="multipart/form-data">
  <ul>
    <li>
      <label for="company_name">Company Name</label>
      <input type="text" name="company_name" id="company_name">
    </li>
    <li>
      <button type="submit">Submit</button>
    </li>
  </ul>
</form>

1 Comment

@DAN You are welcome. My suggestion is far easier for you to maintain. It will show one error at a time and focus the field in error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.