Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Field Not Null, Length, Character Type 1

Status
Not open for further replies.

mswilson16

Programmer
Nov 20, 2001
243
US
Hi All,

I have wrote an app in asp with a sql back ground. Now I need to validate some fields. I have a Name, Code and PhNumber field...

*I need to make sure that Name has something in it,
*Code field has something in it and has to be 4 characters long,
*and need to make sure that phnumber has something in it has only 4 characters and is only numbers...

I want the validation to take place when the form is submitted..

Thanks in advance

Mswilson
 
For text fields, you can test if value is equal or not equal to an empty string ("").

JavaScript text has has a property called length which can be compared to an integer value.

JavaScript has a function called isNaN() which returns true if the string-in-question is not a number. I can't remember if you use it like:

myString.isNaN();

...or...

isNaN(myString);

...but you can google it up and figure it out.

'hope that helps.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
For text fields, you can test if value is equal or not equal to an empty string ("").

JavaScript text has has a property called length which can be compared to an integer value.

JavaScript has a function called isNaN() which returns true if the string-in-question is not a number. I can't remember if you use it like:

myString.isNaN();

...or...

isNaN(myString);

...but you can google it up and figure it out.

'hope that helps.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Double-posted. The second time in two days.

Maybe I should play 2-2-2 in tonight's "Pick 3".


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
You mean, that's NOT what you're supposed to do?

We had an alarm here once that rang three times, twice in succession.

beep-beep-beep
beep-beep-beep

It prefaced general announcements. We called the alarm the "Three-three's", but it took me a while to determine the logic of the name. Shouldn't it have been "Two-Three's" since there were only two groupings?

Finally, I realized that the alarm was THREE beeps/THREE beeps. Thus the title.

Now... er... tell me what you know.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Forgot to add that I am new to Java and dont have a clue how to write the function... or were to call it from...

Sorry! :-(
 
So... you have no javascript so far? (going back to Dan's original question).

We can talk you through it, but we need to know where to start from.

P.S., Lesson 1 - don't say 'Java' when you mean 'JavaScript'


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
sorry guys... right from the beginning would be great. I have the asp but have never wrote javascript, and all I know about javascript is that it runs client side which is what I need...
 
When you run your ASP, it outputs HTML.

View Source on your ASP output, and copy-and-paste the HTML you see between [ignore]
Code:
[/ignore] tags here.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
mswilson16, here is a sample for you.

I use several functions to do the validation.

The validation function is called when the Submit button is pressed on the form due to the onsubmit handler in the <Form> tag and it is looking for a return response.

In the fieldvalidate function I grab the values of each of the form fields to test by their ID. It is important that each field have and ID tag for this to work. This can be modified to use a name tag but using ID is much better supported.

I have three functions for doing different types of validation. any_whole_number, persons_name and set_length.
The set_length function accepts the value to test but also two parameters, the minimum length the string can be and the maximum length the string can be. This way you can use the same routine for other fields that might need to be of a different length. By setting min and max to 4 I insure that it is exactly 4 characters, no more, no less.

I also use a trim function called from within the set_length function so that any leading and trailing spaces are removed prior to testing the value.

If a field fails to validate it pops up an alert then returns false to the onsubmit so the form does not send. If all fields pass it returns true and your page submits to whatever the action is set to.

I setup the functions this way so that you could easily use one or multiple validation tests.

Code:
<HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript">

function formvalidate() {
  var frmName = document.getElementById('Name').value;
  var frmCode = document.getElementById('Code').value;
  var frmPhone = document.getElementById('Phone').value;

  if (!persons_name(frmName)) {
    alert('Invalid Name entered');
    return false;
  }

  if (!set_length(frmCode,4,4)) {
    alert('Invalid Code entered');
    return false;
  }

  if (!set_length(frmPhone,4,4) || !any_whole_number(frmPhone)) {
    alert('Invalid Phone entered');
    return false;
  }
  return true;
}

function any_whole_number(value) {
  var re = /^-?\d+$/;
  return re.test(value);
}

function persons_name(value) {
  //ALlows Upper and lower case letters, hyphen, period and single quote.  Length constrained to 60 characters.
  var re = /^[a-zA-Z'-.\s]{1,60}$/;
  return re.test(value);
}

function set_length(value, min, max) {
  //Test number of characters in value against min and max values.  If no value set for min it defaults 0 if no value for max it defaults to the length of value.
  var vmin=(min == '')?0:min;
  var vmax=(max == '')?value.length:max;
  var re=((trim(value).length < vmin) || (trim(value).length > vmax))?false:true;
  return re;
}

function trim(value) {
  return value.replace(/^\s+|\s+$/g, '');
}
</script>
</HEAD>
<BODY>
<form name="myform" id="myform" action="mypage.asp" method="post" onsubmit="return formvalidate()">
  Name: <input type="text" name="Name" id="Name"><br>
  Code: <input type="text" name="Code" id="Code"><br>
  Phone: <input type="text" name="Phone" id="Phone"><br>
  <br>
  <input type="submit" name="Submit" value="Submit"><br><br>
</form>
</BODY>
</HTML>

Paranoid? ME?? WHO WANTS TO KNOW????
 
BTW, empty fields will never be null, just empty.
I did not test them to not be empty, just to be of a specific length once any leading and trailing spaces are removed. You can use:
if (frmName.length == 0) {
alert('Name field was empty');
return false;
}
Or substitute other field names there for the same effect.
The above is not necessary for the Name field though as the regular expression I used will not allow an empty field to pass anyway.

BTW Part 2. Special thanks to BabyJeffy as he posted the trim function this morning in another post. :)

I did not incorporate the trim function into the set_length or persons_name functions. It all depends really on what you need but if you want it there the easy way to do it is to alter the if statment to this:
if (!persons_name(trim(frmName))) {



Paranoid? ME?? WHO WANTS TO KNOW????
 
hey guys... Thank you very much for your help!

The code for the Name is working great!!! It turns out that I dont need the code validation... but the Phone is not working. It doesn't give me an error, it just goes trhough....

any ideas?
 
Works fine here. Perhaps something was changed when you saved it?
Are you trying my exact code or have you integrated it into your own form? Could be a typo or the form field ID does not match in the script and in the form, etc.


Paranoid? ME?? WHO WANTS TO KNOW????
 
THANKS GUYS!!!! Got it working... not sure what was wrong... but just copied the frmName line, and changed it to frmPhone

Thanks again guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top