473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

making a text field mandatory?

MT
Hi all, this sounds like an easy enough thing to do, but after
spending 45 minutes searching google and various javascript sites I
can't find out how to make a textfield (textbox or whatever you want
to call it) in an html form be a mandatory field. I'm guessing it
would be on the "submit" click, and a pop-up would appear saying to
enter data in teh field. Can anyone recommend a good javascript site?
thanks in advance.
Jul 20 '05 #1
4 22723
On 8 Oct 2003 18:09:02 -0700, in comp.lang.javascript
mr********@hotmail.com (MT) wrote:
| Hi all, this sounds like an easy enough thing to do, but after
| spending 45 minutes searching google and various javascript sites I
| can't find out how to make a textfield (textbox or whatever you want
| to call it) in an html form be a mandatory field. I'm guessing it
| would be on the "submit" click, and a pop-up would appear saying to
| enter data in teh field. Can anyone recommend a good javascript site?
| thanks in advance.


There are a couple of ways:
(client side) use a onSubmit function to test the contents of the
field before moving onto another page. The downside of this is if the
user has turned off javascripting.
(server side). Submit the form and get the server to validate the
data. Downside: can be lengthy delay (server busy/slow connection) in
the user receiving feedback about the errors on the page.

Which is best? Depends. I prefer to use client-side validation where
possible as this
a) allows immediate response to errors and
b) the client machine is rarely that busy (compared to a web server).

Coding differences:
(Client side)
<Form name="f1" action="next.asp" onSubmit="return CheckInputs();">

--- if CheckInputs returns false then you stay on the current page.
--- if CheckInputs returns true then you move to the designated page.

javascript function
function CheckInputs()
{
var OK = true;
if( [check data on some input field] == "" )
{
OK = false;
alert( "Error");
}
return OK;
}

Server side
<Form name="f1" action="next.asp" method="post">

next.asp (JSCRIPT) code
<%
var eNbr = 0;
var v1 = new String(Request.Form("myInput")) + "";
if( v1 == "" )
eNbr++;
..
..
..
if( eNbr )
Response.redirect("nextErrorPage.asp");
%>
---------------------------------------------------------------
jn****@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------
Jul 20 '05 #2
Jeff North <jn****@yourpantsbigpond.net.au> writes:

[client-side vs server-side validation]
Which is best? Depends. I prefer to use client-side validation where
possible as this
a) allows immediate response to errors and
b) the client machine is rarely that busy (compared to a web server).


The optimal solution will do both.

As you point out, if Javascript is not available, the field can not be
validated on the client. In that case, the server *must* check that the
mandatory field has been filled, or it will be processing invalid data.

On the internet, you cannot assume anything about the client. A
malicious user, even with Javascript active, can turn it off or alter
it to avoid your client-side validation.

Client-side validation should never be anything but a service to the
user, preventing a lengthy round trip to the server by warning about
errors before submitting. If the user don't want that service, he can
turn it off. The real validation should happen on the server in any
case.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Give something like this a try:

<script type="text/javascript">

// regular expression to detect one or more spaces
var blankRE=/^[\s]+$/;

// 1-16 letters only
var textRE=/^[a-zA-Z]{1,16}$/;

function ValidateForm(FormObject)
{
// pass form object reference to function
// 'this' or document.forms['myform']

// check to see if required field 'textfield 1' has been completed,check
for empty field and spaces entered into field
if(FormObject.elements['textfield1'].value=="" ||
blankRE.test(FormObject.elements['textfield1'].value))
{
alert(' You have not completed a required field "textfield 1".
Please complete it now.');
FormObject.elements['textfield1'].focus();
return false;
}

// check format of string in textfield 1, allows only 16 upper/lower
case letters
if(!textRE.test(FormObject.elements['textfield1'].value))
{
alert('The contents of "textfield 1" are invalid. Only 16 letters
are allowed.');
FormObject.elements['textfield1'].focus();
return false;
}
return true;
}
</script>

<form name="myform" method="post" action="reqform.php" onsubmit="return
ValidateForm(this);">
<label for="textfield1">textfield 1</label>
<input type="text" name="textfield1" id="textfield1" size="16">
<input type="submit" value="submit form">
</form>

"MT" <mr********@hotmail.com> wrote in message
news:fb**************************@posting.google.c om...
Hi all, this sounds like an easy enough thing to do, but after
spending 45 minutes searching google and various javascript sites I
can't find out how to make a textfield (textbox or whatever you want
to call it) in an html form be a mandatory field. I'm guessing it
would be on the "submit" click, and a pop-up would appear saying to
enter data in teh field. Can anyone recommend a good javascript site?
thanks in advance.

Jul 20 '05 #4
MT
thanks for everyone's help.

"Richard Hockey" <ri***********@dsl.pipex.com> wrote in message news:<3f**********************@news.dial.pipex.com >...
Give something like this a try:

<script type="text/javascript">

// regular expression to detect one or more spaces
var blankRE=/^[\s]+$/;

// 1-16 letters only
var textRE=/^[a-zA-Z]{1,16}$/;

function ValidateForm(FormObject)
{
// pass form object reference to function
// 'this' or document.forms['myform']

// check to see if required field 'textfield 1' has been completed,check
for empty field and spaces entered into field
if(FormObject.elements['textfield1'].value=="" ||
blankRE.test(FormObject.elements['textfield1'].value))
{
alert(' You have not completed a required field "textfield 1".
Please complete it now.');
FormObject.elements['textfield1'].focus();
return false;
}

// check format of string in textfield 1, allows only 16 upper/lower
case letters
if(!textRE.test(FormObject.elements['textfield1'].value))
{
alert('The contents of "textfield 1" are invalid. Only 16 letters
are allowed.');
FormObject.elements['textfield1'].focus();
return false;
}
return true;
}
</script>

<form name="myform" method="post" action="reqform.php" onsubmit="return
ValidateForm(this);">
<label for="textfield1">textfield 1</label>
<input type="text" name="textfield1" id="textfield1" size="16">
<input type="submit" value="submit form">
</form>

"MT" <mr********@hotmail.com> wrote in message
news:fb**************************@posting.google.c om...
Hi all, this sounds like an easy enough thing to do, but after
spending 45 minutes searching google and various javascript sites I
can't find out how to make a textfield (textbox or whatever you want
to call it) in an html form be a mandatory field. I'm guessing it
would be on the "submit" click, and a pop-up would appear saying to
enter data in teh field. Can anyone recommend a good javascript site?
thanks in advance.

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Paul Kooistra | last post by:
I need a tool to browse text files with a size of 10-20 Mb. These files have a fixed record length of 800 bytes (CR/LF), and containt records used to create printed pages by an external company. ...
7
by: Tony Cooke | last post by:
Hi all. I'm not sure why I'm having problems with this but if I try to retrieve the value of a readonly text form I get back that the object is undefined. The reason the text is readonly is...
2
by: tom | last post by:
Hello, I have an asp.net web application where I want people to register themselves. This takes them through a few pages with prev/next functionality. In one of the pages they provide a...
13
by: TC | last post by:
Folks Is there >>ANY<< way to get the actual text of an error that is trapped by the Form_Error event? I mean actual text like: "duplicate record in table XYZ", not template text like:...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
11
by: plumba | last post by:
Hi I have a user enolment form which is users fill out to let IT know that they need setting up on 'x' systems. When the user put a cross in the 'I would like Email' checkbox I would like the...
5
by: JJ297 | last post by:
How do I validate a text box field which only requires a number in it?
3
by: Gaurav Jhamb | last post by:
I have 2-3 forms.On my first form when i serach to put any word it sends results and after that i click on new button in my search i got different organisation names. on the secong form i passed the...
6
by: Clive Backham | last post by:
I need to set up forms where some input fields are mandatory and others are optional. A common convention is to place an asterisk on the labels of the mandatory fields. Of course, I can just hard...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.