I have a page where a user can edit some values (name, description, etc...) which works fine except when a long string is added as a description.
When a user changes the value of an input field, a javascript function is called to check if any values are different from the initial (when the site is first loaded with PHP the initial values are stored as variables). It looks like this:
<script>
//checks if a field has changed value, if yes, change class
function check(div, text){
if(div.val()===text){
div.parent().removeClass("has-warning");
return 0;
} else{
div.parent().addClass("has-warning");
return 1;
}
}
</script>
*here goes the html stuff*
<script>
//whenever the form content changes
$('#horse1').bind('input', function() {
//the form div
var horseDiv = $('#horse1');
//count all the fields with changed value
var error = check(horseDiv.find($('#name')), 'Rinaldo')
+check(horseDiv.find($('#race')),'-')
+check(horseDiv.find($('#date')),'1988-01-01')
+check(horseDiv.find($('#use')), 'Showpferd')
+check(horseDiv.find($('#shortDesc')), 'short Description')
+check(horseDiv.find($('#description')),'This can be a long text over multiple lines');
//if there is at least one field with a
//changed value, display an error message
var errorDiv = horseDiv.find($('#notSaved'));
if(error==0){
errorDiv.hide();
} else{
errorDiv.show();
}
});
</script>
So firstly if anyone has an idea how to make this behavior more elegant, I am open to suggestions.
But the problem I'm having is that this script completely stops working if the text (the description) is longer than one line. I am not sure why or how to solve this, so any help would be greatly appreciated.
***UPDATE: Compare multiple line texts in javascript
In case anyone has the same problem, here is the solution that worked for me:
var textNew = text.replace(/%0D/g, '');
var divNew = encodeURIComponent(div.val()).replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
The Variable "text" is the rawlurlencode() String from the php-Script and div.val() is the plaintext read from the textarea. With the above code, textNew===divNew returns true, even if they contain commas, umlaute, new lines and other nasty stuff