Link to home
Start Free TrialLog in
Avatar of Darius
DariusFlag for Lithuania

asked on

trim(), match() functions for validation

Guys,
I need to solve an issue with validation. I have code below and I need to validate
entered value.
Accepted values is alpha, numeric or '-'(hyphen) characters.


input:                     '  00ab-12cd34000   '
expected result:  'ab-12cd34000'                  // => should not give a warning.

input:                    '  00ab-12cd3_&*%^£$4000   '
expected result:  'ab-12cd3_&*%^£$4000'  // => warning - Invoice ID have invalid characters


var invoiceId;
        invoiceId = document.getElementById("txtId").value.trim(); // trailing and leading white-spaces
        var invId = invoiceId.match(/0+([\w\-]+)/); // alphanumeric and  '-' (hyphen) characters only
        var invMatch = invoiceId.match(/0*(.*)/); // any characters

// If no value or value is white-space
        if (invoiceId == "") {
            warn = "!" + "Please, enter the Invoice ID.\n";
            document.body.style.cursor = 'default';
            document.getElementById("btnSend").onclick = '';
             }  

// if entered value have not (alpha, numeric or '-'(hyphen)) characters => gives warning
        else if (invId != invMatch) {
            warn = "!" + "Invoice ID has invalid characters.\n";
            document.body.style.cursor = 'default';
            document.getElementById("btnSend").onclick = '';
        }
. . .

Open in new window


Thank you!
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Darius

ASKER

Kim, Julian Thank you! Solved

var invoiceId;
var matchInvId;
invoiceId = document.getElementById("txtMessageId").value.trim();
matchInvId = invoiceId.match(/0+([\w\-]+)/);

     if (invoiceId == "") {
            alert ("Please enter the Invoice ID");
        }
     else if (!matchInvId) {
            alert ("Please enter a valid Invoice ID");
        }
     else if (matchInvId[1].toString().length > 10) {
            alert ("Invoice ID has more then 10 characters.");
        }
. . .

But!!!  I have more questions...  :)
I using trim function above to remove leading trailing white-spaces
What function stand for to remove white-space inside string?
input:                     '  00 ab      - 1  2c d 3 400 0   '

These does not working for me...
        var invoiceId_1 = invoiceId.replace(" ", "");
        var invoiceId_2 = invoiceId.replace(' ', '');
        var invoiceId_3 = invoiceId.toString().replace(" ", "");
        var invoiceId_4 = invoiceId.toString().replace(' ', '');
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way, the following string would NOT produce an error with this routine.
"  00 ab      _ 1  2c d 3 4 &  "

Open in new window

Avatar of Darius

ASKER

Perfect! Works

Kim,
 you mentioned Regex
      What is difference between these expression:
(/0+([\w\-]+)/)
(/[^a-zA-Z\d-]/)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
/[^a-zA-Z\d-]/ looks for any character that is NOT alpha (a-zA-Z) and NOT numeric (\d) and NOT a dash (-).

/0+([\w\-]+)/ looks for a string that begins with 1 or more zeros (0+) followed by one or more word characters or dashes ([\w\-]+). The one or more word characters or dashes is isolated in parentheses and produces a separate match in the match array.
Avatar of Darius

ASKER

updated code!
       Thanks Kim

var invoiceId;
var matchInvId;
invoiceId = document.getElementById("txtMessageId").value.replace(/ /g, "");
matchInvId = invoiceId.match(/0+([\w\-]+)/); 

     if (invoiceId == "") {
            alert ("Please enter the Invoice ID");
        }
     else if (matchInvId[0] !== invoiceId) {
            alert ("Please enter a valid Invoice ID");
        }
     else if (matchInvId[1].toString().length > 10) {
            alert ("Invoice ID has more then 10 characters.");
        }
. . .

Open in new window

Sidebar
/0+([\w\-]+)/
Assumes string starts with a 0 if the string does not always start with a 0 then
/0*([\w\-]+)/
Avatar of Darius

ASKER

Tank you Julian