开发者

Jquery - Need to validate text field with selected words

I need to validate input text filed with below values , "PO BOX", POBOX, "GPO BOX" and "GPOX"

if someone enter those words into text box, alert them "We do not ship products to PO Box addresses". and remove entire text.

I have build below,

$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').blur(function(){

$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val() == $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('PO BOX'),

$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val() == $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('POBOX'), $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val() == $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('GPO BOX'),

$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val() == $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('GPOX'),

$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val() == $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('po box'),

$('#objCheckoutReg开发者_如何转开发istration_PhysicalAddress_txtAddressLine1').val() == $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('pobox'),

$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val() == $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('gpo box'),

$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val() == $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('gpox') {

          alert('We do not ship products to PO Box addresses');
          $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val('');
          $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').focus();
      }

  });     });

but problem is it's only validating exact word,

I mean if add PO BOX 1145 and some other text, it wan't validate.


Try using a regex. I'm not regex guru, but you just need to look for text which has 'PO' followed by 'BOX' somewhere in your string.

So, this should do the trick

var regex = new RegExp("PO");
var match = regex.exec($('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val());
if (match != null)
{
    alert('We do not ship products to PO Box addresses');
    $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val('');
    $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').focus();
}


your problem is that you compare the match result to the original value. if you change your lines from: $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val() ==$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('GPO BOX'),

to:

$('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1').val().match('GPO BOX'),

it will work fine. offcourse you can use regex matching with grouping for a oneliner validation, but current will work

(-: Y.


Try writing a regular expression. that will work in your case.....

var str="GPO 123";

var patt=/PO/g;

var result=patt.test(str);//returns true or false

document.write("Returned value: " + result);


You can check for containment using the String.indexOf(...) function.

var input = $('#objCheckoutRegistration_PhysicalAddress_txtAddressLine1');
var inputValue = input.val().toUpperCase();

var poBoxIdentifiers = ['PO BOX', 'POBOX', 'GPOX'];

for(var i = 0; i < poBoxIdentifiers.length; i++) {
    if (inputValue.indexOf(poBoxIdentifiers[i]) !== -1) {
        alert('We don\'t ship to...');
        input.val('').focus();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜