Solved

Javscript validation of submission form

Posted on 2012-03-15
12
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

717 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