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 610483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:40:49+00:00 2026-05-13T17:40:49+00:00

I have written a set of javascript functions that allow me to validate user

  • 0

I have written a set of javascript functions that allow me to validate user input on a form. I only want to accept valid input, and impose the following behaviour:

When a user enters an invalid form, I display an alert and inform them that the value entered is incorrect. Crucially, the original (valid) value in the form is not changed.

The value is only changed when the value has been validated.

For example, suppose I want to accept only positive integers in a field.

This is the sequence of events that describes the desired behaviour.

Scenario 1 (valid input)

  1. Form loads with valid default in the input field
  2. User types in valid number
  3. Input field value is updated (as per normal form behaviour)

Scenario 2 (INvalid input)

  1. Form loads with valid default in the input field
  2. User types in INvalid number
  3. Alert box is shown alert(‘Invalid value’)
  4. Input field value is NOT CHANGED (i.e. the value is the same as BEFORE the user typed in the invalid number)

[Edit]

The only problem I am facing at the moment (i.e. what this question is seeking an answer for), is Scenario 2, action point 4. More specifically put, the question degenerates to the following question:

How do I stop the value of a field from changing, if I (somehow) determine that the value being entered by the user is invalid. This is really, all I’m trying to answer.

I am also doing server side checks, this question is just about the front end – i.e. refusing to change a field (form text input) value if I determine that the value is incorrect.

BTW, I am using jQuery, and would like to implement this in a manner that separates behaviour from display (I think this is what is meant by the term ‘unobtrusive’ ?)

Any suggestions on how to implement this behaviour as described above, would be very much appreciated.

PS: I dont want to use yet another jQuery plugin for this. I should be able to use jQuery + the simple javascript validation functions I have already written.

  • 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-05-13T17:40:49+00:00Added an answer on May 13, 2026 at 5:40 pm

    When loading the page, couldn’t you create a hidden form value or js variable in which you store the initial/default value for the field? When they change the form field, validate it, and if it passes, update the hidden field or js variable to match that in the field they updated.

    When the input given by the user fails validation, show the invalid entry along with the error message and then update the form field back to the value you have saved which would be either the
    default value or the last valid value that they entered.

    EDIT:
    Note that this is only a quick and (very) dirty example of doing what I explained in my answer above. If you have a lot of inputs, you will probably want to store the values in an associative array instead of in hidden form values, but this should give you a good handle on what I am suggesting. I would also strongly encourage you to NOT use alert boxes for notification of invalid answers.

    <html>
    <head>
    <script type="text/javascript">
        function validate()
        {
            var field1 = document.getElementById("field1");
            var saved  = document.getElementById("field1_save");
            if (field1.value < 0 || field1.value > 10)
            {
                alert("Field1 value of " + field1.value + " is invalid");
    
                // Change the value back to the previous valid answer
                field1.value = saved.value;
                return false;
            }
    
            // Save the valid input
            saved.value = field1.value;
            return true;
        }
    </script>
    </head>
    
    <body>
    Test User Input
    
    <form name="form1"   id="form1" method="post">
    <input name="field1" id="field1" type="text" value="2" onblur="validate();"/>
    <input name="field1_save" id="field1_save" type="hidden" value="2" />
    
    <input name="btnSubmit" type="submit" value="Submit" />                             
    </form>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The current directory (in the unix sense) in an app… May 15, 2026 at 5:04 pm
  • Editorial Team
    Editorial Team added an answer I have spent some more time doing some testing, and… May 15, 2026 at 5:04 pm
  • Editorial Team
    Editorial Team added an answer Is it possible to call inflate multiple times to dynamically… May 15, 2026 at 5:04 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.