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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.