Add length of string into Error Message and also test for string trimmed

HI,

Please help , I am a java script dunce. (better on the server side)
I want to include the actual  string length in the error message for Notes in the code below.
Also in the example below how would I test for the length trimmed - then put up the error, in case the user has
copied in a lot of spaces. I strip these off on the server side. I tried various things and it did not
work.


$('#changeHeadline').click(function(){
				var error = "";
				var headline = $("#headline").val();				
				var notes = $("#notes").val();
				
				if (!($.trim(headline).length)) {
					var error = error +'Please enter headline for your add<br>'
				}
				if (!($.trim(notes).length)) {
					var error = error + 'Please enter your advertisement for housesitting<br>'
				}	
				if (notes.length >= 4000) {
					var error = error  + 'You have exceeded the 4,000 character limit,  Please edit your Text and try again.<br>'
				}	
				if (headline.length >= 120) {
					var error = error + 'Headline for housesitting must be bellow Max 120 Characters, Please edit your Text and try again.<br>'
				}		
				if(error.length){
					var UIAlertDialogApi = function () {			
						handleAlerts();
						function handleAlerts() {				
							Metronic.alert({
								container: '#bootstrap_alerts_demo', // alerts parent container(by default placed after the page breadcrumbs)
								place: 'append', // append or prepent in container 
								type: 'danger',  // alert's type
								message: error,  // alert's message
								close: true, // make alert closable
								reset: true, // close all previouse alerts first
								focus: true, // auto scroll to the alert after shown
								closeInSeconds:0, // auto close after defined seconds
								icon:'warning' // put icon before the message
							});
						}
					}();
				} else{
					$('#changeHeadlineForm').submit();
				}				
			});
			$('#cancel').click(function(){
				$('.alert-success').hide();
				$('#cancelMessage').show();
				setTimeout(function() {
					window.location.href = window.location.href;
				}, 1000);
			});
		</script>		

Open in new window

Ian WhiteOwner and FounderAsked:
Who is Participating?
 
chaauCommented:
In javascript you do not need to convert numbers to strings, it will let you concatenate strings with numbers:
if (notes.length >= 4000) {
					var error = error  + 'You have exceeded the 4,000 character limit, You have entered ' + notes.length + ' characters, Please edit your Text and try again.<br>'
				}

Open in new window

I did not understand your second question
0
 
Ian WhiteOwner and FounderAuthor Commented:
HI Thanks that works for the message.

notes may include spaces before and after. The user could have copied the text in including leading and following spaces.  I remove these on the server side. However I didn't want to put an error up if the notes.length counted the leading and trailing spaces.  So I could either trim off the spaces - how to do using java script in the example below OR test for the string excluding spaces?
0
 
chaauCommented:
Just execute this as the first statement:
$('#changeHeadline').click(function(){
				$("#notes").val($("#notes").val().trim());

Open in new window

It will trim the value of the "notes" textbox
0
 
Ian WhiteOwner and FounderAuthor Commented:
Excellent Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.