/    Sign up×
Community /Pin to ProfileBookmark

Simple form validation

Hi,

I am trying to add some form validation which is fine for simple checking for blanks but am now trying to do something more complex.

I want to do a couple of things and am not sure how to do them. I understand that regular expression is really powerful but really do not understand it. My first problem is that I do not want to allow a discount data unless it is in a format which I want.

The format I want is:

(NAME-WEEKS-DISCOUNT)

Name can be as long as they want in terms of characters
Weeks should be a number
Discount should be a number

The main thing here is that I want them to be seperated by a hyphen ‘-‘ as I am formatting this string based on this symbol.

In addition to this I also have another field which I want to format just to check if it is blank. The problem here is that the field is added dynamically.

My initial thought was to find out the total amount of fields which have been added dynamically and loop through checking these for “”.

Is there a better way of doing this.

Here is an example of the code itself so that you can see the way that the levels in this case are added.

The form:

[CODE]
<div id=”dynamicInput”>
<input type=”button” value=”Add level” onClick=”addInput(‘dynamicInput’);”>
</div>
<hr/>
[/CODE]

The JS:

[CODE]
<script language=”javascript” type=”text/javascript” defer>
// This function will dynamically add level fields to the form.
var counter = 1;
var limit = 10;
function addInput(divName)
{
if (counter == limit)
{
alert(“You have reached the limit of adding ” + counter + ” inputs”);
}
else
{
var newdiv = document.createElement(‘div’);
newdiv.innerHTML = “<label>Level”+ (counter)+ “: *</label><input type=’text’ name=’levels[]’>”;
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
[/CODE]

Any advice would be greatly received!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@thraddashMay 10.2010 — This is my attempt for the discount validation part...

[CODE]<input id="data" type="text" value="(A Name-1234-5678)" />
<input type="button" value="Check Value" onclick="validateValue();" />

<script type="text/javascript">
function validateValue()
{
if (!/^([w ]+-d+-d+)$/.test( document.getElementById('data').value )) {
alert('Invalid Value');
} else {
alert('All Ok');
}
}
</script>[/CODE]


Hope it helps ?
×

Success!

Help @shifter480 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...