Javscript validation of submission form

I have a submission page with validations to ensure all areas of the form are correctly submitted. There is a section of the submission form where the user must enter a correct case number. If entered correctly (checks a text file), the name of a debtor appears (in the debtor field). If they enter a case number incorrectly or enter random numbers, they will receive a "CASE NUMBER NOT FOUND" in place of the debtor name. The minimum number of characters required in the case number field is 10. If they enter 10 numbers/characters regardless if it is valid or not, they are able to continue. I do not want this to happen.

What I want the validation to do with the case number field:
1. Ensure a case number's format is entered correctly - 8:09-bk-00234? (If Possible)
2. Make sure at least 10 numbers are entered

What I want the validation to do with the debtor field:
1. If debtor field is blank, do not allow to submit
2. If debtor field returns CASE NUMBER NOT FOUND, do not allow to submit

Below is a snippet of the validation that currently works, but obviously needs tweaking.

Any assistance with code is GREATLY appreciated. Thank you!

if (theForm.case_number.value == "")
{
alert("Please enter your full case number in the \"Case Number\" field");
theForm.case_number.focus();
return (false);
}

if (theForm.case_number.value.length < 10)
{
alert("Please enter at least ten characters in the \"Case Number\" field");
theForm.case_number.focus();
return (false);
}


if (theForm.debtor.value == "")
{
alert("Debtor information is invalid. Please enter the correct Case Number in the \"Case Number\" field");
theForm.debtor.focus();
return (false);
}

Open in new window

arendt73Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ahoffmannCommented:
if (/^[0-9a-zA-z:-]{5}$/.test(theForm.case_number.value)===false){alert("wrong format");}
if (theForm.debtor.value=="CASE NUMBER NOT FOUND"){return false;}
0
arendt73Author Commented:

Open in new window

Thanks ahoffmann. This appears very close. I've attached your modified code for review.

The issue I am getting is that the form will not submit, even after a correct case number and format is entered into the Case_Number field.

One thing regarding the Debtor field, it is hidden and when a correct number is entered (obviously with the correct format), it displays the name of a person or company. So even with those paramters met, the form will not submit.  

Thank you again for your assistance. It is of great help to me.

if (/^[0-9a-zA-z:-]{5}$/.test(theForm.case_number.value)===false)
{
alert("wrong format1");
theForm.case_number.focus();
return (false);
}

if (theForm.debtor.value=="CASE NUMBER NOT FOUND")
{
alert("wrong format2");
//theForm.case_number.focus();
return (false);
}

Open in new window

0
ahoffmannCommented:
> .. even after a correct case number ..
oops, you need to adapt the {5} in my posted regex, as it is the number of characters allowed

also, it's probably a good idea to make a very simple sanitation first and remove white spaces:
  theForm.case_number.value = theForm.case_number.value.replace(/\s*/g,'')
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

arendt73Author Commented:
ahoffman, what do you mean be "need to adapt the {5} in my posted regex, as it is the number of characters allowed." Can you show this in code to me? Thank you.
0
ahoffmannCommented:
if the length of your number is i.e. 13, then use:
if (/^[0-9a-zA-z:-]{13}$/.test(theForm.case_number.value)===false){alert("wrong format");}

if you expect 10, 11, 12 or 13 characters, then use
if (/^[0-9a-zA-z:-]{10,13}$/.test(theForm.case_number.value)===false){alert("wrong format");}
0
arendt73Author Commented:
Thank you. I will test shortly. What does the .test (in the code) accomplish?
0
arendt73Author Commented:
I attempted the suggestions and they do not work.

Again, in the case_number field, the format must be: #:##-bk-##### or as an example if someone typed out the number: 8:09-bk-00234. If this specific criteria is not met, then an alert should appear and not allow the user to continue.

Also, if the debtor field returns either a blank or CASE NUMBER NOT FOUND, then that should prompt an alert and should not allow the user to continue until they have entered a correct case number.

Thank you for the assistance and I look forward to viewing new or updated recommendations in resolving my issue.
0
ahoffmannCommented:
> Again, ...
if(/^[0-9a-zA-z:-]{13}$/.test("8:09-bk-002342")===false){alert("wrong format");}
> Also, ...
if (theForm.debtor.value=="CASE NUMBER NOT FOUND"){alert("false");}
if (theForm.debtor.value=="CASE NUMBER NOT FOUND"||/^\s*$/.test(x)===true){alert("false");}

both work for me
if you use these checks, you need to deside if you want to return true or false from your funtion
0
Christopher KileCommented:
I would correct ahoffman's first pattern to:

if (/^[0-9][:][0-9]{2}[-]bk[-][0-9]{4}$/.test(theForm.case_number.value)===false)
{
alert("wrong format1");
theForm.case_number.focus();
return (false);
}

The earlier pattern did not account for the 'bk' in the center of the case number, and this pattern exactly matches the pattern of the case number as you've described, whereas the previous pattern also matches '-------------' or ':::::::::::::'.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahoffmannCommented:
I would correct the correction :)

if (/^[0-9][:][0-9]{2}[-]bk[-][0-9]{5}$/.test(theForm.case_number.value)===false)
0
Christopher KileCommented:
LOL yes, I miscounted.  Your correction is correct.
0
arendt73Author Commented:
Solution given acceptable.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.