-2

I open this subject for second time.

However there is no solution.

Below code does not work. I can enter empty(Space) value.

var veri = {
YeniMusteriEkleTextBox: $('#YeniMusteriAdiTextbox_I').val(),
};
if (!veri.YeniMusteriEkleTextBox) {
alert("Customer name can not be empty!");
}
user2706372
  • 95
  • 3
  • 3
  • 10
  • 3
    Don't open new identical questions – mplungjan Sep 13 '13 at 06:41
  • @mplungjan it's not strictly identical - the answers to the other question (from which this code came) failed to mention the need to `trim` the input to remove leading and trailing whitespace – Alnitak Sep 13 '13 at 06:42
  • 2
    Regardless, the correct approach is clearly to edit the other question. There is no interaction from the OP on the other question at all. – Chris Hayes Sep 13 '13 at 06:43

4 Answers4

2

You need to .trim the value to remove leading and trailing white space:

var veri = {
    YeniMusteriEkleTextBox: $('#YeniMusteriAdiTextbox_I').val().trim()
};

The .trim method doesn't exist on some older browsers, there's a shim to add it at the above MDN link. Since you're using jQuery you could also use $.trim.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • So answer the other question and leave this one to rot - and use $.trim() to not have to use the shim – mplungjan Sep 13 '13 at 06:43
  • @mplungjan in general I recommend using shims instead of adhoc library replacements for "standard" ES5 functions so as to encourage use of those standard APIs as and when they're supported "out of the box" by newer browsers. – Alnitak Sep 13 '13 at 08:00
  • If you already use jQuery there is absolutely no need to add a shim in my opinion. jQuery already uses .trim if available and a shim if not – mplungjan Sep 13 '13 at 08:13
1

You need to remove all the whitespaces to achieve that

var veri = {
    YeniMusteriEkleTextBox: $('#YeniMusteriAdiTextbox_I').val().replace(/^\s+|\s+$/g, '')
};
if (!veri.YeniMusteriEkleTextBox) {
    alert("Customer name can not be empty!");
}

UPD: Some additional information

Your object

var veri = {
    YeniMusteriEkleTextBox: $('#YeniMusteriAdiTextbox_I').val(),
};

has a comma after last key-value pair. This can cause an error in IE.

Right ways is:

var obj = {
    key1: 'val1',
    key2: 'val2' // no comma after last pair
}
Alex
  • 1,336
  • 10
  • 19
0

Have you tried:

if (veri.YeniMusteriEkleTextBox!='') {
alert("Customer name can not be empty!");
}
eggshot
  • 190
  • 6
  • That would be very silly unlesss you meant to use it as a debug to see if the statement was executed at all – mplungjan Sep 13 '13 at 07:09
0

Try this

var veri = {
YeniMusteriEkleTextBox: $('#YeniMusteriAdiTextbox_I').val(),
};
if (veri.YeniMusteriEkleTextBox.trim()=="" || veri.YeniMusteriEkleTextBox==undefined) {
alert("Customer name can not be empty!");
}
Amit
  • 15,217
  • 8
  • 46
  • 68