Click to See Complete Forum and Search --> : Simple Questions
amacfarl
10-03-2003, 07:34 AM
Folks,
I am new to JavaScript, but not new to Java. I have a few questions:
1) How do I validate that a field contains only text or numbers?
2) I have two submit buttons in my form. The form has onSubmit="return check()". In the check() method I validate the form. I also what to find out which submit button was clicked. How do I do this?
To develop Java programs I have several resources which outline what methods and attrbiutes are available. In JavaScript however I dont. In addition, when I make a mistake in Java I get an error message, whilst when I make a mistake in JavaScript, I get nothing, just the code does not run.
Is there any recommendation that you have that will make developing JavaScript easier.
Thanks in advance
Angus
1) If the if statement evaluates, the form fields contains letters, numbers, and the understrike. Other characters will cause it to break.
if (/^[\w\d]+$/.test(document.formname.fieldname.value)) {
2) You could pass an argument to the function when you call it:
<input type="button" value="Go 1" onclick="someFunction('1');">
<input type="button" value="Go 1" onclick="someFunction('2');">
Now, you can just check if 1 or 2 is set, and you'll know which buttons was clicked.
3) Mozilla and Netscape have a JavaScript debugger called Venkman. Mozilla 1.4 comes with it, but for older versions, you might have to download separatly. Also, in Mozilla/Netscape, you can type javascript# in the addressbar to get errors.
Charles
10-03-2003, 07:53 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<script type="text/javascript">
<!--
function validate (f) {
alert (f.submitButton.name)
return false;
}
// -->
</script>
<form action="" onsubmit="return validate(this)">
<div>
<label>Foo<input type="text" onchange="if (!/^[\d\w]+$/.test(this.value)) {alert('That would not appear to be a valid foo.'); this.value=''; this.focus()}"></label>
<button name="submit1" onclick="this.form.submitButton = this" type="submit">Submit</button>
<button name="submit2" onclick="this.form.submitButton = this" type="submit">Submit</button>
</div>
</form>
amacfarl
10-03-2003, 08:01 AM
Thanks to you both!
I have one followup question...
if (/^[\w\d]+$/.test(document.formname.fieldname.value))
Can you direct me do any further information about the use of /^[\w\d]+$/ syntax. I.e. is it any between the /'s.
Therefore... if I what to exclude £,!&¬ would the syntax be
if (/£,!&¬.text(field))
return false // field contains incorrect values
else
return true
THANKS!
Charles
10-03-2003, 08:14 AM
See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html but be careful. A full 13% of users do not use JavaScript. It's important to make sure that your page sitll works for those good people.
You'd want to add additional chacters between the [ and the ] as that is used to define the character set. Take a look at the link Charles provided for more information.
amacfarl
10-03-2003, 08:34 AM
THANKS!
You both have been a great help