-2

I have an ISBN lookup. It has to be 10 characters. I have a custom validation rule to check that. But a lot of my customers are copying and pasting and when they do they tend to get an extra space at beginning or end. So I want to check for whitespace before or after the string and update it FIRST. And THEN run the regex. If it fails (not alpha numeric or more than 10 digits) then show error message.

Here's my current custom validation rule:

jQuery.validator.addMethod(
"isbn10rule",
function(value, element) {
    var isValid = /^[a-zA-Z0-9]{10}$/.test(value);
    return this.optional(element) || isValid;
},
"Must be exactly 10 characters and only letters and numbers"
);

I'm dont want answers that test against a scope restricted value. I need the value updated BEFORE the validation is even ran.

For example: String is " 0940087537"; If I try to paste that into the field, it immediately says "Must be exactly 10 characters and only letters and numbers". But if it stripped off that space first, then it would see there are only 10 characters left.

swg1cor14
  • 1,682
  • 6
  • 25
  • 46
  • Trim the input before running a regex: `/^[a-zA-Z0-9]{10}$/.test(value.trim());` – Wiktor Stribiżew Nov 15 '16 at 15:21
  • @WiktorStribiżew how do I down vote you? You obviously didnt read the question. I'm not just trying to trim whitespace. I know how to do that. I wanted to auto UPDATE the element before it ran the regex....not just run the regex on the trimmed value. – swg1cor14 Nov 15 '16 at 15:25
  • Then trim and update the element value. its simple as that. – Abdul Rehman Nov 15 '16 at 16:14

2 Answers2

1

You can use trim() method of Javascript like this:

function(value, element) {
    var trimmed_value = value.trim();
    var isValid = /^[a-zA-Z0-9]{10}$/.test(trimmed_value);
    return this.optional(element) || isValid;
}

The trim() method removes whitespace from both sides of a string.

Note: The trim() method does not change the original string.

Hope this helps!

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45
  • Appreciate that Saumya but as you said....the trim method does not change the original string. I want the original string updated before validation even runs. – swg1cor14 Nov 15 '16 at 15:32
-1

Trim the value first and update the element. Then test the same value.

function(value, element) {
    var trimmed_value = value.trim();
    $(element).val(trimmed_value);
    var isValid = /^[a-zA-Z0-9]{10}$/.test(trimmed_value);
    return this.optional(element) || isValid;
}
swg1cor14
  • 1,682
  • 6
  • 25
  • 46
  • You can also access the data before it is even pasted. Using the clipboard API from the event. http://stackoverflow.com/questions/11605415/jquery-bind-to-paste-event-how-to-get-the-content-of-the-paste – Garis M Suero Nov 15 '16 at 16:23
  • You are going to downvote me because I didn't even know you could bind a paste event? Been doing this for years and never heard of it. I don't think it deserves a downvote. – swg1cor14 Nov 16 '16 at 16:20