Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Tidy with Ruby

Ruby interface to HTML Tidy Library Project (tidy.sf.net).

  require 'tidy'
  Tidy.path = '/usr/lib/libtidy.so'
  html = '<html><title>title</title>Body</html>'
  xml = Tidy.open(:show_warnings=>true) do |tidy|
    tidy.options.output_xml = true
    puts tidy.options.show_warnings
    xml = tidy.clean(html)
    puts tidy.errors
    puts tidy.diagnostics
    xml
  end
  puts xml


output
true
line 1 column 1 - Warning: missing <!DOCTYPE> declaration
line 1 column 7 - Warning: plain text isn't allowed in <head> elements
Info: Document content looks like XHTML 1.0 Transitional
2 warnings, 0 errors were found!

<html>
<head>
<meta name="generator"
content="HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org" />
<title>title</title>
</head>
<body>Body</body>
</html>



Note: Couldn't get it to run on Ubuntu version 7.10 or 7.04 (LoadError: no such file to load -- tidy
), however it ran fine on Gentoo.

reference: http://tidy.rubyforge.org/

*update 18-Mar-08 16:15*
I got it working on Ubuntu I simply needed to add - require 'rubygems'

ValidateForm()

// ValidateForm()

    Private Function ValidateForm() As FormValidity

        Dim MyReturn As New FormValidity
        Dim ReturnString As New StringBuilder
        Dim IsValidForm As Boolean = True

        If (txtFirstName.Text = "") Then
            ReturnString.Append("First Name")
            txtFirstName.Attributes.Add("style", "background-color:#EBECED")
            txtFirstName.Focus()
            IsValidForm = False
        End If

        If (txtLastName.Text = "") Then
            ReturnString.Append(", Last Name")
            txtLastName.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtLastName.Focus()
            IsValidForm = False
        End If

        Dim InputEmail As String = txtEmail.Text
        Dim EmailExpression As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        Dim Re As New Regex(EmailExpression)

        If (Not Re.IsMatch(InputEmail)) Then
            ReturnString.Append(", E-mail Address")
            txtEmail.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtEmail.Focus()
            IsValidForm = False
        End If

        If (txtAdd1.Text = "") Then
            ReturnString.Append(", Address 1")
            txtAdd1.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtAdd1.Focus()
            IsValidForm = False
        End If

        If (txtCity.Text = "") Then
            ReturnString.Append(", City")
            txtCity.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtCity.Focus()
            IsValidForm = False
        End If

        If (ddlState.Text = "") Then
            If (txtStateOther.Text = "") Then
                ReturnString.Append(", State")
                ddlState.Attributes.Add("style", "background-color:#EBECED")
                If (IsValidForm) Then ddlState.Focus()
                IsValidForm = False
            End If
        End If

        If (txtZip.Text = "") Then
            ReturnString.Append(", Zip")
            txtZip.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtZip.Focus()
            IsValidForm = False
        End If

        If (ddlCountry.Text = "") Then
            ReturnString.Append(", Country")
            ddlCountry.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then ddlCountry.Focus()
            IsValidForm = False
        End If

        If (txtPhone.Text = "") Then
            ReturnString.Append(", Phone")
            txtPhone.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtPhone.Focus()
            IsValidForm = False
        End If

        MyReturn.IsValid = IsValidForm

        If (ReturnString.ToString.StartsWith(",")) Then
            MyReturn.FormFields = ReturnString.ToString.Substring(1)
        Else
            MyReturn.FormFields = ReturnString.ToString
        End If

        Return MyReturn

    End Function

Validate email and domain

// Vlidate email and domain name
<?php

function validate_email($email){

$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";

if(eregi($exp,$email)){
	if(checkdnsrr(array_pop(explode("@",$email)),"MX")){
		print("$email is ok.<br>");
			}else{
			print("$email is ok. But domain is not.<br>");
			}
				}else{
				print("$email is not ok.<br>");
				}   
}

validate_email("shantanu.ok");
validate_email("shantanu.ok@gmail.com");
validate_email("shantanu.ok@fsjaldkfjlsfjsljflsfjsldk.com");

?>
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS