Want to validate if the last character of a field is '+'

suvro
Mega Sage
Mega Sage

Hi,

I want to validate if the last character of a field is '+' sign or not.

How can I achieve that?

Thanks and Regards,

Chandra

3 REPLIES 3

Gurpreet07
Mega Sage

if(a.charAt(a.length-1) != '+'){


        alert('Message') ;


return false ;


}


santoshsahoonis
Kilo Guru

This would do it:



    var str = "Hello world!-";


      var res = str.substring(str.length-1, str.length );


if(res == '+')


alert( 'we got a +');


else


alert('no +...');


Mihir Mohanta
Kilo Sage

slice(-1) function returns the last character of a string.



Example:



var sd = g_form.getValue('short_description');


  alert(sd.slice(-1));



You can get the last character of the string and compare it is "+" or not.



Thanks,


Mihir