Darius
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
Thank you!
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 = '';
}
. . .
Thank you!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By the way, the following string would NOT produce an error with this routine.
" 00 ab _ 1 2c d 3 4 & "
ASKER
Perfect! Works
Kim,
you mentioned Regex
What is difference between these expression:
(/0+([\w\-]+)/)
(/[^a-zA-Z\d-]/)
Kim,
you mentioned Regex
What is difference between these expression:
(/0+([\w\-]+)/)
(/[^a-zA-Z\d-]/)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
/[^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.
/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.
ASKER
updated code!
Thanks Kim
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.");
}
. . .
Sidebar
/0+([\w\-]+)/
Assumes string starts with a 0 if the string does not always start with a 0 then
/0*([\w\-]+)/
/0+([\w\-]+)/
Assumes string starts with a 0 if the string does not always start with a 0 then
/0*([\w\-]+)/
ASKER
Tank you Julian
ASKER
var invoiceId;
var matchInvId;
invoiceId = document.getElementById("t
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().
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().repla
var invoiceId_4 = invoiceId.toString().repla