Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:54:48+00:00 2026-06-15T08:54:48+00:00

I’m trying to validate some form fields using JS functions, but they don’t seem

  • 0

I’m trying to validate some form fields using JS functions, but they don’t seem to load or check the field when the submit button is clicked.

<html>
  <body>
    <?php require_once('logic.php'); ?>
    <h1>Add new Region/Entity</h1>
    <?php 
      if ($_POST['submitted']==1) {
        echo $error;
      }
    ?>
    <form action="logic.php" method="post" name="registration" onSubmit="return formValidation();">
      Region: <input type="text" name="region" value="<?php echo @$_POST['region']; ?>"><br>
      Description: <br><textarea name="description" cols="50" rows="10"><?php echo @$_POST['description']; ?></textarea><br>
      Remarks: <input type="text" name="remarks" value="<?php echo @$_POST['remarks']; ?>">
      <input type="hidden" name="submitted" value="1"><br>
      <input type="submit" value="Submit" onclick="">

And here’s the JS:

<script type="text/javascript">
  function formValidation() {
    var region = document.registration.region;
    var descr = document.registration.description;
    var remarks = document.registration.remarks;
    if(empty_validation()) {
      if(reg_validation()) {
        if(reg_letters()) {
          if (desc_letters()) {
            if (desc_validation()) {

            }
          }
        }
      }
    }
    return false;
  }
  function empty_validation() {
    var reg_len = region.value.length;
    var descr_len = descr.value.length;
    var rem_len = remarks.value.length;
    if (reg_len == 0 || decr_len == 0 || rem_len == 0) {
      alert("Please fill all the fields");
      return false;
    }
    return true;
  }
  function reg_validation() {
    if (reg_len > 50) {
      alert("Only 50 characters are allowed in the Region field");
      region.focus();
      return false;
    }
    return true;
  }
  function reg_letters() {
    var letters = /^[A-Za-z]+$/;
    if(region.value.match(letters)) {
      return true;
    } else {
      alert('Region field must have only alphabetical characters');
      region.focus();
      return false;
    }
  }
  function desc_validation() {
    if (descr_len > 500) {
      alert("Only 500 characters are allowed in the description field");
      descr.focus();
      return false;
    }
    return true;
  }
  function desc_letters() {
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
    for (var i = 0; i < descr_len; i++) {
      if (iChars.indexOf(descr.value.charAt(i)) != -1) {
        alert ("Your description has special characters. \nThese are not allowed.\n Please remove them and try again.");
        return false;
      }
    }
    return true;
  }
</script>
</form>
</body>
</html>

As you can see, I’m posting values from the form to a PHP file. And I’ve used the onSubmit event in the form tag to initialize the formValidation() function. It isn’t working for some reason.

Here’s a working example if you need one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:54:49+00:00Added an answer on June 15, 2026 at 8:54 am

    I’m not going to fix every little error you have, but the main problem is that variables aren’t defined where you expect them to be. You can’t declare a variable in one function and expect it to be available in the next (without passing it as a parameter or declaring it globally). For example, your empty_validation function should be this:

    function empty_validation(region, descr, remarks) {
        var reg_len = region.value.length;  
        var descr_len = descr.value.length;  
        var rem_len = remarks.value.length;  
    
        if (reg_len == 0 || decr_len == 0 || rem_len == 0) {
            alert("Please fill all the fields");  
            return false;  
        }
        return true;  
    }
    

    And in your formValidation function, you call it like this:

    if(empty_validation(region, descr, remarks)) {
    

    This is just one example, and you would need to do it in a few places.

    A few other things – you always return false from formValidation…did you mean to put return true; inside the innermost if statement? Also, since all you seem to be doing is calling each of those functions, you can probably put them into one big if statement, like this:

    if (empty_validation() && reg_validation() && reg_letters() && desc_letters() && desc_validation()) {
        return true;
    }
    return false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.