Solved

Javscript validation of submission form

Posted on 2012-03-15
12
222 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

17 Experts available now in Live!

Get 1:1 Help Now