Solved

Javascript validation submission page with special field

Posted on 2012-03-20
3
338 Views
Last Modified: 2013-11-19
I need to validate a text field (case_number) in an ASP submission form that contains the following format:

#:##-bk-#####

example: 5:12-bk-12345
or             3:11-bk-99345

Is there anyway that Javascript can validate this specific format? The amount of characters will always be no less than 13, and the : - bk - will always remain a constant.

Any help is greatly appreciated. I attempted the following but it does not work.

Thank you!

if (/^[0-9a-zA-z:-]{13}$/.test(theForm.case_number.value)===false)
{
alert("Case Number format is not correct");
theForm.case_number.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
3 Comments
 
LVL 18

Accepted Solution

by:
nap0leon earned 400 total points
ID: 37745467
I'm sure there's a regex that would work great, but I am incapable of delivering one of them to you.  If no one steps forward with one, you can use this approach in the meantime:

you can split the string into an array, then check the length of the array and the value of each character one at a time.
var str = theForm.case_number.value;
strArray = str.split(''); //this creates an array for every character in the string

Open in new window


or you can examine the characters one at a time or in clumps - regex for checking each of these as being all numbers should be easy to come by
// string.substr(start,length)
var str = '5:12-bk-12345'
var sectionOne = str.substr(0,1) //should be a number
var sectionTwo = str.substr(1,1) //should be a :
var sectionThree = str.substr(2,2) //should be 2 numbers
var sectionFour = str.substr(4,4) //should be -bk-
var sectionFive = str(substr(8,5) //should be 5 numbers

Open in new window


Out of time for the night - hope this helps point you in a workable direction.
0
 
LVL 13

Assisted Solution

by:ansudhindra
ansudhindra earned 100 total points
ID: 37745560
check this

http://jsfiddle.net/Sudhindra/Xmf7f/

var reg = new RegExp("^[0-9][:][0-9]{2}-bk-[0-9]{5}$");
    alert(reg.test(v1));

if it returns true then it is valid, if it is false it is not valid.
0
 

Author Closing Comment

by:arendt73
ID: 37986806
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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

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