1

I am trying to validate the data of the input text if the existing value is changed then validate and throw error. Though I am able to capture the default value and validate with the new changed value. However, if the user goes back and change to the old value again how would I handle or capture that? Seems like default value will change once the value is changed, how can I preserve the original value of that input text regardless of any multiple changes happens on the input value?

//Note: This function will only triggers with on submit of the form
function A() {
    var firstName = document.getElementById('firstName');
    var holdFirstName = firstName.defaultValue;

    if (firstName.value === firstName.defaultValue) {

        return false;
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sam
  • 127
  • 2
  • 15
  • The default value should never change due to user input. Will be whatever it is in the value attribute – charlietfl Jan 11 '18 at 00:59
  • I am actually retrieving the values from the database and populating in the fields and then trying to validate if any values in the field have been changed by the user, if yes then validate which value has changed? – Sam Jan 11 '18 at 01:01
  • 1
    Populating how? Setting value property with javascript or setting strings in value attribute? – charlietfl Jan 11 '18 at 01:06
  • Is the requirement for the form to be submitted only if the default value is changed by user action? – guest271314 Jan 11 '18 at 01:07
  • This is an update page and user can only be able to submit if they update any of the 6 fields. If no field value is changed then they cannot submit and the error message will be displayed. I am looking for a logic to preserve the old value and if the user decides to revert back to the old value i should be able to retrieve it. – Sam Jan 11 '18 at 01:14
  • Well either you are setting values from an array in client(browser) or setting values as strings in attributes. If by string the default won't change and if from array you should still have access to that data – charlietfl Jan 11 '18 at 01:28
  • i am setting it as String. I have updated above the code snippet, though the logic now executes if any if the value is not changed but when i go back and change it to default value then am still getting an error. The error should throw only is two values are equal, if they are not equal then should submit. – Sam Jan 11 '18 at 01:33
  • `defaultValue` will always be what the original value attribute was in the HTML tag. If you're having trouble with that, you can just store whatever the first value is as a `data-attribute` like "data-default" when you initially populate the fields. – skyline3000 Jan 11 '18 at 02:33
  • @skyline Cool that worked, thanks! How would I preserve the original dropdown value? – Sam Jan 11 '18 at 03:03
  • What dropdown value? Your question only mentions a text input. – skyline3000 Jan 15 '18 at 14:05

0 Answers0