2

I have multiple input fields with different values. I can edit them but cannot be empty (if empty show an alert). When I am editing if I edit them with blank spaces i used trim to not allow that.But,I am able to edit the input with more spaces at the start and end with a word in between.I don't need this.

How to achieve the following ?

a)Should not start with space.

b)No spaces after the word,if i have one word.

<input type="text" class="selector" value="new">
<input type="text" class="selector" value="old">
<input type="text" class="selector" value="newest">
<input type="text" class="selector" value="oldest">
<input type="text" class="selector" value="older">

$('.selector').on('blur',function () {
  var current_value = $.trim($(this).val());
  $(this).attr('value',current_value);
  console.log(current_value);
  if ($('.selector[value="' + current_value + '"]').not($(this)).length > 0 || current_value.length == 0 ) {
    $(this).focus();
    alert('You cannot use this');
  }
});

My fiddle here

6
  • Please include the relevant JavaScript in your question also, as Roko has kindly done on your behalf this time. Commented Dec 8, 2015 at 5:00
  • 1
    Thanks for edit @Roko.@David it is in my fiddle. Commented Dec 8, 2015 at 5:02
  • And it should be in your question, not (only) in the Fiddle. Don't make us chase around the internet for the pleasure of helping you. If your code is hosted externally and that site falls over, dies or is reorganised, then the question becomes nonsensical at best, and is of no use to anyone else in future. Commented Dec 8, 2015 at 5:05
  • @DavidThomas Sure from next question.Thanks Commented Dec 8, 2015 at 5:06
  • @IamRaviteja : Check the answer my me. Is it what you were looking for? Commented Dec 8, 2015 at 5:46

5 Answers 5

1

You should use this

$(this).val(current_value);

instead of this

$(this).attr('value', current_value);
Sign up to request clarification or add additional context in comments.

3 Comments

working fine in my fiddle but not in my real project
If not working, then explain what problem you are facing @IamRaviteja
@MyWay Sorry! my mistake, I used uppercase letter instead of lower
0

Check if first index of space is 0 or last index is end of string then show alert.

Add the following Condition:

if($(this).val().indexOf(" ") == 0 || $(this).val().lastIndexOf(" ") == ($(this).val().length -1) ){
       alert('You cannot use this');
    }

Check updated fiddle

or Snippet:

$('.selector').on('blur', function() {
  var current_value = $.trim($(this).val());
  $(this).attr('value', current_value);
  console.log(current_value);
  if ($('.selector[value="' + current_value + '"]').not($(this)).length > 0 || current_value.length == 0) {
    $(this).focus();
    alert('You cannot use this');
  } else
  if ($(this).val().indexOf(" ") == 0 || $(this).val().lastIndexOf(" ") == ($(this).val().length - 1)) {
    alert('You cannot use this too');

  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="selector" value="new">
<input type="text" class="selector" value="old">
<input type="text" class="selector" value="newest">
<input type="text" class="selector" value="oldest">
<input type="text" class="selector" value="older">

Comments

0

See the updated fiddle

$('.selector').on('blur',function () {
    $(this).val($.trim($(this).val()));
    if($(this).val() == "") {
      alert("Empty values not allowed!");
      this.focus();
    }
});

Explanation:
First trim the values, then check if empty. If empty raise the alert and focus the box

Comments

0

I would use input pattern attribute:

<input type="text" class="selector" value="new" pattern="^[^\s]+(\s+[^\s]+)*$" />

Regex found in this answer.

Comments

0

To remove spaces at the start and get the value (removes all spaces from left and right edges of the string.):

var value = $.trim($("input").val());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.