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

  • SEARCH
  • Home
  • 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 8536453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:40:04+00:00 2026-06-11T10:40:04+00:00

I am trying to create a generic Javascript function that I can use to

  • 0

I am trying to create a generic Javascript function that I can use to verify different field values and output the error message alongside the correct field. I would like to create an efficient, reusable function, but I am new to Javascript and am unsure how to proceed.

Advise on how to change my validation function to validate both firstname and lastname would be appreciated.

My code so far:

function validateForm() {
    var x = document.forms["myForm"]["firstname"].value;
    var reg_azs = /^[^a-zA-Z\-']+$/;
    var reg_oal = "!#$%^&*()+=[]\\\';,{}|\":<>?123456790";

    if (x == null || x == "") {
        document.getElementById("fn").innerHTML = "This fuild is required.";
        return false;
    } else if (reg_azs.test(firstname.value)) {
        document.getElementById("fn").innerHTML = "Only alphabetic letters.";
        return false;
    } else {
        for (var i = 0; i < x.length; i++) {
            if (reg_oal.indexOf(x.charAt(i)) != -1) {
                document.getElementById("fn").innerHTML = "Only alphabetic letters.";
                return false;
            }
        }
    } else {
        document.getElementById("fn").innerHTML = "correct";
        return true;
    }
}

My Form:

<form name="myForm" action="connection.php" method="post">
    <label for='firstname'>First Name:</label>
    <input type="text" id="firstname" name="firstname" onchange="return validateForm()" />
    <err1 id="fn"></err1>
    <br>  

    <label for='lastname'>Last Name:</label>
    <input type="text" id="lastname" name="lastname" onchange="return validateForm()" />
    <err1 id="ln"></err1>
    <br>
</form>
  • 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-11T10:40:05+00:00Added an answer on June 11, 2026 at 10:40 am

    I would make the function accept a few parameters: id of the element being examined, id of the element to show the error message, and then maybe a string of regular expression to validate it (could be optional).

    From there, you could set x as:

    var x=document.getElementById(param1).value;
    

    and everytime you reference the error element, like this:

    document.getElementById("fn")
    

    change it to:

    document.getElementById(param2)
    

    So your function declaration would look like this:

    function validateForm(param1, param2) {
    

    And when you call it, it would look like:

    onchange="return validateForm('firstname', 'fn');"
    onchange="return validateForm('lastname', 'ln');"
    

    and so on.

    You’d probably want to change the parameter names as well, param1 and param2 are just for example, and might be better as targetElem and errorLabel, respectively.

    UPDATE:

    This design is also narrow-minded, such that you have to call validateForm for every element you want to validate. An alternative, to allow multiple elements to be validated with one function call is to use an array of objects, where each object has a form like:

    {"element_id": "whatever", "error_id": "whatever"}
    

    But in your function, you would loop through the single parameter (an array), and access each one like so:

    for (var i = 0; i < param1.length; i++) {
        // Use param1[i]["element_id"] and param1[i]["error_id"]
    }
    

    In this case, you can add extra things to each object, to allow for specific validation rules, such as not being empty, at least a certain length, no longer than a certain length, etc. …and in the loop, you’d have to check for those things being present.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm basically trying to create a generic object in javascript that I can use
I'm trying to use MvcHtmlString.Create to create a JavaScript variable. However the output is
I'm trying to create a generic function that removes duplicates from an std::vector. Since
I'm trying to create a generic function in grails that will allow me to
Trying to create a generic video plugin for Expression Engine 2 where I can
I'm trying to create a generic class in PHP that will provide a way
I was trying to create a generic Dictionary that implements IXmlSerializable (credit to Charles
I am trying to create a generic method that will read an attribute on
I am trying to create a generic extension that uses 'TryParse' to check if
I'm trying to create a generic list from a specific Type that is retrieved

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.