Home > Mobile >  Compare text input's value and default value and check if changes has only spaces
Compare text input's value and default value and check if changes has only spaces

Time:06-06

User changes the value of that text box.If user want to close form I must check if changes(recent.value)contains only spaces and if not - just close form, but if it has - alert("Do you realy want to exit without saving changes?")

$('.cancel-button').on('click', function () {
    if (recent.value != recent.defaultValue) {
        // if changes I must check - if this changes contains only spaces
    }
});

CodePudding user response:

Look at below method isSpaceEqual, which removes spaces in data before comparing, returns true when the change is only spaces.

function isSpaceEqual(value1, value2) {
    value1 = String(value1 || "");
    value2 = String(value2 || "");
    return value1.replace(/[ \r\n]/g, "") === value2.replace(/[ \r\n]/g, "");
}
$('.cancel-button').on('click', function () {
    if (recent.value != recent.defaultValue) {
        // if changes I must check - if this changes contains only spaces
        if(isSpaceEqual(recent.value, recent.defaultValue)) {
            // change is only spaces, new line or carriage return
        } else {
            // actual change
        }
    }
});
  • Related