?
Solved

Javscript validation of submission form

Posted on 2012-03-15
12
Medium Priority
?
239 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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 

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
 

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 1200 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 300 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

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

These seven tips can help you create an extraordinary website, one that captivates audiences and has them wanting to return regularly for more. Keep reading to find out what your site is missing and what you need to add!
I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

590 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