Solved

Javscript validation of submission form

Posted on 2012-03-15
12
223 Views
Last Modified: 2012-05-18
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

0
Comment
Question by:arendt73
  • 5
  • 5
  • 2
12 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37726737
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
 

Author Comment

by:arendt73
ID: 37727079

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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37728341
> .. 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
 

Author Comment

by:arendt73
ID: 37728988
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37729034
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
 

Author Comment

by:arendt73
ID: 37729198
Thank you. I will test shortly. What does the .test (in the code) accomplish?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:arendt73
ID: 37739938
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37740013
> 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
 
LVL 23

Accepted Solution

by:
Christopher Kile earned 400 total points
ID: 37749805
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 100 total points
ID: 37750094
I would correct the correction :)

if (/^[0-9][:][0-9]{2}[-]bk[-][0-9]{5}$/.test(theForm.case_number.value)===false)
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 37772712
LOL yes, I miscounted.  Your correction is correct.
0
 

Author Closing Comment

by:arendt73
ID: 37986802
Solution given acceptable.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now