473,387 Members | 1,705 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.

check all textfields

Hi there,

I'm building a multi-language PHP/mySQL -site.
I'm also building a CMS for the site.

There are 5 languages. In the CMS fields for e.g. english
bodytext are called (id=) 'bodytext_en'.
German looks like 'bodytext_ge', etc.

The '_en' part tells us that it's a textfield for english texts.
How can i check if ALL english fields are filled out, without
defining each and every field separately.
Like:

if(document.(allfields.'_en').value == ''){
alert('Hey!')
};

Thanks!

Jul 23 '05 #1
3 1579
frizzle wrote:
How can i check if ALL english fields are filled out, without
defining each and every field separately.


Fully untested quickhack:

function checkComplete(sLangIdentifier) {
var i, j, nForms, nElems, oCurForm, oCurElem, aTitles, rLang;
rLang = new RegExp("_" + rLang + "$", "i");
nForms = document.forms.length;
for (i=0; i<nForms; i++) {
oCurForm = document.forms[i];
nElems = oCurForm.elements.length;
for (j=0; j<nElems; j++) {
oCurElem = oCurForm.elements[j];
if ((oCurElem.type == "text" || oCurElem.type == "textarea")
&& oCurElem.name.match(rLang)
&& oCurElem.value == ""
) {
aTitles[aTitles.length]= oCurElem.title;
}
}
}
if (aTitles.length) {
alert(
"Information for " +
sLangIdentifier +
" is not complete!\n\n" +
"Please fill out the following inputs:\n" +
aTitles.join("\n")
);
}
}

ciao, dhgm
Jul 23 '05 #2
Dietmar Meier wrote:
frizzle wrote:
How can i check if ALL english fields are filled out, without
defining each and every field separately.

Fully untested quickhack:

function checkComplete(sLangIdentifier) {
var i, j, nForms, nElems, oCurForm, oCurElem, aTitles, rLang;
rLang = new RegExp("_" + rLang + "$", "i");
nForms = document.forms.length;
for (i=0; i<nForms; i++) {
oCurForm = document.forms[i];
nElems = oCurForm.elements.length;
for (j=0; j<nElems; j++) {
oCurElem = oCurForm.elements[j];
if ((oCurElem.type == "text" || oCurElem.type == "textarea")
&& oCurElem.name.match(rLang)
&& oCurElem.value == ""
) {
aTitles[aTitles.length]= oCurElem.title;
}
}
}
if (aTitles.length) {
alert(
"Information for " +
sLangIdentifier +
" is not complete!\n\n" +
"Please fill out the following inputs:\n" +
aTitles.join("\n")
);
}
}

ciao, dhgm


Elegant, but it seems to me that a better way is to have one form and
change the text labels depending on the language chosen. Only one
set of form elements is required with one set of validation rules.

Multiple error messages are required, maybe multiple messages for
each language but I've allowed for one per selected language.

Multiple classes are used on elements to show how to use styles in
this context. Note that the 'en' class doesn't need to be defined.

The reset and onload functions should account for page re-loads and
form reset as well - it's easy to get the selection and labels out of
sync.

Onclick is used for the radios rather than onchange because of IE's
implementation where onchange doesn't do anything until the control
loses focus (as per the spec, but different to Geko browsers and
confusing to many).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>test</title>
<style type="text/css">
..ge {display: none;}
..fr {display: none;}
</style>
<script type="text/javascript">

function getLang(f){
var b = f.elements['lang'];
var i = b.length;
while (i-- && !b[i].checked );
return b[i].value;
}

function changeLang(f,lang){
var div = document.getElementById(f.name + 'labels');
var s = div.getElementsByTagName('span');
var i = s.length;
lang = new RegExp('\\b'+lang+'\\b');
while (i--){
s[i].style.display =
(lang.test(s[i].className))? 'inline' : 'none';
}
}

function resetLang(f) {
var x = f.elements['lang'];
var i = x.length;
while ( i-- && !x[i].checked );
x[i].click();
}

function checkForm(f){
var errObj = {
en : 'English error message...',
ge : 'German error message...',
fr : 'French error message...'
}
var els = f.elements;
var i = els.length;
while (i--){
el = els[i];
if ( 'text' == el.type || 'textarea' == el.type ){
// do validation
if ( '' == el.value ) {
alert(errObj[getLang(f)]);
return false;
}
}
}
return true;
}

window.onload = function () {
document.forms['formA'].reset()};
</script>
</head>
<body>
<form action="" name="formA" onsubmit="return checkForm(this);">
<div id="formAlabels">
Select a language:<br>
English<input type="radio" name="lang" value="en" checked
onclick="changeLang(this.form,this.value)">
&nbsp;&nbsp;German<input type="radio" name="lang" value="ge"
onclick="changeLang(this.form,this.value)">
&nbsp;&nbsp;French<input type="radio" name="lang" value="fr"
onclick="changeLang(this.form,this.value)">
<br>
<input type="text" name="userName" size="30">
<span class="en">Name (english)</span>
<span class="ge">Name (german)</span>
<span class="fr">Name (french)</span>
<br>
<textarea name="userAddr" rows="5" cols="30"></textarea>
<span class="en">Address (english)</span>
<span class="ge">Address (german)</span>
<span class="fr">Address (french)</span>
<br>
<input type="reset" onclick="
this.form.reset();
resetLang(this.form);
">
<input type="submit" value="Send details...">
</div>
</form>
</body>
</html>
--
Rob
Jul 23 '05 #3
RobG wrote:

Agggh, forgot to update the posted script, here is the one with
multiple classes:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>test</title>
<style type="text/css">
..ge {display: none;}
..fr {display: none;}
..label {font-family: sans-serif; color: #666699;}
..tip {font-family: sans-serif; color: #9999dd; font-size: 80%;}
</style>
<script type="text/javascript">

function getLang(f){
var b = f.elements['lang'];
var i = b.length;
while (i-- && !b[i].checked );
return b[i].value;
}

function changeLang(f,lang){
var div = document.getElementById(f.name + 'labels');
var s = div.getElementsByTagName('span');
var i = s.length;
lang = new RegExp('\\b'+lang+'\\b');
while (i--){
s[i].style.display =
(lang.test(s[i].className))? 'inline' : 'none';
}
}

function resetLang(f) {
var x = f.elements['lang'];
var i = x.length;
while ( i-- && !x[i].checked );
x[i].click();
}

function checkForm(f){
var errObj = {
en : 'English error message...',
ge : 'German error message...',
fr : 'French error message...'
}
var els = f.elements;
var i = els.length;
while (i--){
el = els[i];
if ( 'text' == el.type || 'textarea' == el.type ){
// do validation
if ( '' == el.value ) {
alert(errObj[getLang(f)]);
return false;
}
}
}
return true;
}

window.onload = function () {
document.forms['formA'].reset()};
</script>
</head>
<body>
<form action="" name="formA" onsubmit="return checkForm(this);">
<div id="formAlabels">
Select a language:<br>
English<input type="radio" name="lang" value="en" checked
onclick="changeLang(this.form,this.value)">
&nbsp;&nbsp;German<input type="radio" name="lang" value="ge"
onclick="changeLang(this.form,this.value)">
&nbsp;&nbsp;French<input type="radio" name="lang" value="fr"
onclick="changeLang(this.form,this.value)">
<br>
<input type="text" name="userName" size="30">
<span class="label en">Name (english)</span>
<span class="label ge">Name (german)</span>
<span class="label fr">Name (french)</span>
<br>
<textarea name="userAddr" rows="5" cols="30"></textarea>
<span class="label en">Address (english)</span>
<span class="tip en">Address tip (english)</span>
<span class="label ge">Address (german)</span>
<span class="tip ge">Address tip (german)</span>
<span class="label fr">Address (french)</span>
<span class="tip fr">Address tip (french)</span>
<br>
<input type="reset" onclick="
this.form.reset();
resetLang(this.form);
">
<input type="submit" value="Send details...">
</div>
</form>
</body>
</html>
--
Rob
Jul 23 '05 #4

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

Similar topics

3
by: R.G. Vervoort | last post by:
I would like to select an option in a pulldown, select a record in a mysql database depending on the pulldown selection and then put the data from the record in the textfields. I can retrieve...
6
by: LRW | last post by:
I have no idea if this is more a PHP question or Javascript question, because my problem hinges equally on both. I have a PHP script that queries a database and creates a list of rows for each...
1
by: Filips Benoit | last post by:
Dear All, After copying a record using an Stored procedure all textfields (nvarchar) has max lenght ! See VBA-code and SP below VBA-code tos execute SP
1
by: Ed | last post by:
Hi, I have an html page with a div element within a form for dynamically creating textfields. The problem is when I click a link on the page, or the submit button, then click the back button,...
1
by: Rich Wahl | last post by:
Ok, This may sound like a noob question, but I have an application, that asks the user to fill in fields from a pre-generated list of possibilities. And upon clicking submit, the user is...
6
by: anirban.anirbanju | last post by:
hi there, i've some serious problem to add rows dynamically in a table. my table contains 5 cell. | check | from_value | to_value | color_text | color_value |...
0
by: chandutp | last post by:
hi i am new to this forum can i populate datagrid items of asp page to another asp page having textfields. in this datagrid userId is the index key by which it referenced.and i am using edit...
1
by: eureka | last post by:
Hi folks, I am working on a webapplication using Jsp and JS. On my main Jsp(Jsp1) I have a table which is created dynamically inside a <divand contains all the backend-table's records as rows,...
3
by: bravephantom | last post by:
In my project "scientific calculator", im using 2 textfields in my GUI app. the problem now (or actually what i dislike) is the user has to use the 2 textfields even if he needs a function of only 1...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.