/    Sign up×
Community /Pin to ProfileBookmark

Very simple validation (well I thought it would be simple!)

Hi sorry I’m a bit of a Java noob and I could really use some help. I’d like to add some very simple validation to this form. All I really want is that if you type in some text and it matches three options, say “red”, “green” or “blue”, it bypasses the confirm() and submits the form. If any text other than the three options is entered you have to OK the confirm() to submit or cancel (like the way the code below currently operates). I’ve never used validation before and it’s proving a real headache!

[CODE]<script>
function convert() {
document.execute.first.value = document.execute.first.value.replace( /s/g, “”);
var first = document.getElementsByName( “first”)[0];
var second = document.getElementsByName( “second”)[0];
second.value = “(“+ first.value +”)”;
msg = document.execute.second.value +” Correct?”
if(confirm(msg)) { location.href=second.value; }
}
</script>

<form name= “execute” onsubmit=’convert();return false;’>
<input name=’first’ type=’text’ /> <input name=’second’ type=’hidden’ />
<input type=’submit’ value=”Execute” />
</form>[/CODE]

I’d really appreciate any help

Thanks!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@nitro2k01Oct 26.2007 — (Javascript, not Java)
&lt;script&gt;
function convert() {
var first = document.getElementsByName( "first")[0],
second = document.getElementsByName( "second")[0];

<i> </i>first.value = first.value.replace( /s/g, "");

<i> </i>second.value = "("+ first.value +")";

<i> </i>switch(first.value){
<i> </i>case 'blue':
<i> </i>case 'green':
<i> </i>case 'red':
<i> </i> return true;
<i> </i> break;
<i> </i>default:
<i> </i> return confirm(second.value + "nnCorrect?");
<i> </i> break;
<i> </i>}
}
&lt;/script&gt;

&lt;form name= "execute" onsubmit='return convert();'&gt;
&lt;input name='first' type='text' /&gt; &lt;input name='second' type='hidden' /&gt;
&lt;input type='submit' value="Execute" /&gt;
&lt;/form&gt;
One thing to note here. This will only test for the lowercase versions of the strings.

Also, do you really need [B]second[/B], the hidden [B]input[/B]? If you don't and I don't think you do, the references to it can be removed.

And before someone comments, I know the [B]break[/B]s aren't needed in this example, but when teaching noobs, it's good practice to always have a break in the end to give them the habit.
Copy linkTweet thisAlerts:
@Angry_Black_ManOct 26.2007 — im happy to see that unecessary "return false" removed with this solution. that makes no sense to be there.
×

Success!

Help @iGirl 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...